尝试从核心数据中的两个实体中删除数据
Try to delete data from two entities in core data
我尝试从两个实体中删除记录。核心数据有两个实体,名称 Student 和 Detail 都具有相反的关系。关系是
Student -> Detail:detail Detail -> Student:student
尝试从 table 视图中删除两个实体的记录。但是当我尝试删除时,只有实体 Student 从 delete 但从 detail Entity 无法删除。它向我显示了这个错误。
-[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];
将您的代码替换为:
- (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 获得委托回调时,您应该将其删除。如果您还没有实施这些方法,那么您现在就实施吧。
请替换
[studentEntity removeDetail:mySet];
和
[studentEntity removeDetailObject:detailEntity];
我尝试从两个实体中删除记录。核心数据有两个实体,名称 Student 和 Detail 都具有相反的关系。关系是
Student -> Detail:detail Detail -> Student:student
尝试从 table 视图中删除两个实体的记录。但是当我尝试删除时,只有实体 Student 从 delete 但从 detail Entity 无法删除。它向我显示了这个错误。
-[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];
将您的代码替换为:
- (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 获得委托回调时,您应该将其删除。如果您还没有实施这些方法,那么您现在就实施吧。
请替换
[studentEntity removeDetail:mySet];
和
[studentEntity removeDetailObject:detailEntity];