首页 文章

尝试从核心数据中的两个实体中删除数据

提问于
浏览
0

我尝试从两个实体中删除记录 . 核心数据有两个实体,名称Student和Detail都具有反比关系 . 关系是

学生 - >详细信息:详细信息 - >学生:学生

尝试从表视图中删除两个实体的记录 . 但是当我尝试删除时,只有实体Student来自删除但来自详细实体不能删除 . 它告诉我这个错误 .

  • [NSSet isSubsetOfSet:]:set参数不是NSSet'
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        // Delete the row from the data source

        AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
        _mainContext = [appDelegate manageObjectContext];

        [_mainContext deleteObject:[self.fetchedResultsController objectAtIndexPath:indexPath]];
        Detail *detailEntity = [self.fetchedResultsController objectAtIndexPath:indexPath];
        Student *studentEntity = [self.fetchedResultsController objectAtIndexPath:indexPath];

        NSMutableSet *mySet = [[NSMutableSet alloc] init];
        [mySet removeObject: detailEntity];
        [studentEntity removeDetail:mySet];
        studentEntity.detail = detailEntity;

        NSError *error = nil;
        if (![_mainContext save:&error]) {
            NSLog(@"Unresolve Error %@, %@", error, [error userInfo]);
            abort();
        }

        [self.tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];

    } 
}

StudentCoredataclass.h

#import "Student+CoreDataClass.h"


NS_ASSUME_NONNULL_BEGIN

@interface Student (CoreDataProperties)

+ (NSFetchRequest<Student *> *)fetchRequest;

@property (nullable, nonatomic, copy) NSString *name;
@property (nullable, nonatomic, copy) NSString *study;
@property (nullable, nonatomic, copy) NSString *number;
@property (nullable, nonatomic, retain) NSSet<Detail *> *detail;

@end

@interface Student (CoreDataGeneratedAccessors)

- (void)addDetailObject:(Detail *)value;
- (void)removeDetailObject:(Detail *)value;
- (void)addDetail:(NSSet<Detail *> *)values;
- (void)removeDetail:(NSSet<Detail *> *)values;

@end
NS_ASSUME_NONNULL_END

DetailCoredataclass.h

#import "Detail+CoreDataClass.h"


NS_ASSUME_NONNULL_BEGIN

@interface Detail (CoreDataProperties)

+ (NSFetchRequest<Detail *> *)fetchRequest;

@property (nullable, nonatomic, copy) NSString *address;
@property (nullable, nonatomic, copy) NSString *contact;
@property (nullable, nonatomic, copy) NSString *email;
@property (nullable, nonatomic, copy) NSString *number;
@property (nullable, nonatomic, retain) Student *student;

@end

FetchedResultController:

AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
    _mainContext = [appDelegate manageObjectContext];
    NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] initWithEntityName:@"Student"];

    // Add Sort Descriptors
    [fetchRequest setSortDescriptors:@[[NSSortDescriptor sortDescriptorWithKey:@"name" ascending:NO]]];

    //[fetchRequest setRelationshipKeyPathsForPrefetching: @"detail"];
    // Initialize Fetched Results Controller
    self.fetchedResultsController = [[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest managedObjectContext:_mainContext sectionNameKeyPath:nil cacheName:nil];

    // Configure Fetched Results Controller
    [self.fetchedResultsController setDelegate:self];

    // Perform Fetch
    NSError *error = nil;
    [self.fetchedResultsController performFetch:&error];

2 回答

  • 1

    用以下代码替换代码:

    - (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
        if (editingStyle == UITableViewCellEditingStyleDelete) {
            AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
            NSManagedObjectContext *context = [appDelegate manageObjectContext];
            Student *studentEntity = [self.fetchedResultsController objectAtIndexPath:indexPath];
            [context deleteObject:studentEntity];
             NSError *error = nil;
            [context save:&error];
        } 
    }
    

    接下来,在模型中设置 Delete Rule ,以便在删除学生时删除详细信息(反之) . 我不清楚为什么你有数据分成两个实体 .

    你不应该在这里删除tableView的Cell . 当你从fetchedResultsController获得委托回调时,你应该删除它 . 如果您尚未实现这些方法,那么现在就这样做 .

  • 0

    请替换

    [studentEntity removeDetail:mySet];
    

    [studentEntity removeDetailObject:detailEntity];
    

相关问题