我尝试从两个实体中删除记录。核心数据有两个实体,名称Student和Detail都具有反比关系。关系是
学生 - >细节:细节细节 - >学生:学生
尝试从表格视图中删除两个实体的记录。但是当我尝试删除时,只有实体学生来自删除但来自详细实体才能删除。它告诉我这个错误。
- [NSSet isSubsetOfSet:]:set参数不是NSSet'
DataAdapter.Fill()
StudentCoredataclass.h
- (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];
}
}
DetailCoredataclass.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
FetchedResultController:
#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
答案 0 :(得分: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获得委托回调时,你应该删除它。如果你还没有实现这些方法,那么现在就这样做。
答案 1 :(得分:0)
请替换
[studentEntity removeDetail:mySet];
与
[studentEntity removeDetailObject:detailEntity];