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

问题描述:

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

Student -> Detail:detail Detail -> Student:student

尝试从表视图中删除这两个实体的记录。但是当我尝试删除时,只有实体学生从删除但从详细实体不能删除。它显示我这个错误。

-[NSSet isSubsetOfSet:]: set argument is not an 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]; 
+0

您要删除的NSMutableSet,而不是一个NSSet中 – ddb

+0

但在我看来,你应该这样做[studentEntity removeDetail:detailEntity]。而不是 – ddb

+0

在哪行代替你的代码[studentEntity removeDetail:detailEntity]; @ddb –

替换为您的代码:

- (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

嘿,但它只从Student实体删除无法从Detail实体中删除。解决这个问题。 –

+0

转到模型对象(它结束.xcdatamodel - 并以可视方式显示您的模型)。选择“学生”实体。接下来选择详细的关系。在右侧面板上选择模型检查器。对于'Delete Rule'选择'Cascade'。这意味着,每当你删除一个学生的所有细节也将被删除。 –

+0

在给予删除规则无法工作后显示相同的错误:[NSSet isSubsetOfSet:]:设置参数不是NSSet –

请更换

[studentEntity removeDetail:mySet]; 

[studentEntity removeDetailObject:detailEntity]; 
+0

告诉我这样的错误:一对一关系的不可接受类型的值:property =“detail”;所需类型=细节;给定类型=学生;值= (实体:Student; id:0xd000000000140000 ; data:{ detail =“0xd000000000140002 “; name = hnfgdfgf; number = 10; study = oioiyhuh; })。' –

+0

其他错误是这样的:[NSSet isSubsetOfSet:]:设置参数不是NSSet' –

+0

你确定detailEntity和studentEntity是机器人绑定并存在于核心数据中吗? – ddb