删除实体的所有条目时未调用 controllerWillChangeContent
controllerWillChangeContent not being called when deleting all entries of an entity
下面是我为 tableView "reset" 按钮实现的方法。我已验证实体中的条目已被删除,但删除后未调用我的 controllerWillChangeContent。有没有办法手动调用这个方法?
CoreDataStack *coreDataStack = [[CoreDataStack alloc]init];
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] initWithEntityName:@"FoodEntry"];
[fetchRequest setIncludesPropertyValues:NO];
NSError *error;
NSArray *fetchedObjects = [coreDataStack.managedObjectContext executeFetchRequest:fetchRequest error:&error];
for (NSManagedObject *object in fetchedObjects)
{
[coreDataStack.managedObjectContext deleteObject:object];
}
error = nil;
[coreDataStack.managedObjectContext save:&error];
编辑:FRC 代码
- (NSFetchedResultsController *)fetchedResultsController {
if (_fetchedResultsController != nil) {
return _fetchedResultsController;
}
CoreDataStack *coreDataStack = [CoreDataStack defaultStack];
NSFetchRequest *fetchRequest = [self entryListFetchRequest];
_fetchedResultsController = [[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest managedObjectContext:coreDataStack.managedObjectContext sectionNameKeyPath:nil cacheName:nil];
_fetchedResultsController.delegate = self;
return _fetchedResultsController;
}
无法确定,因为不了解 CoreDataStack
的内部结构,但看起来您正在使用两个不同的堆栈:请注意您的代码使用:
CoreDataStack *coreDataStack = [[CoreDataStack alloc] init];
而 FRC 使用:
CoreDataStack *coreDataStack = [CoreDataStack defaultStack];
您可以更改您的代码以使用与 FRC 相同的代码(defaultStack
可能是一个单例,并且将 return 调用时使用相同的堆栈)。但是如果FRC和delete代码在同一个class,那么直接使用FRC的managedObjectContext:
会更容易(也更清晰)
NSManagedObjectContext *context = self.fetchedResultsController.managedObjectContext
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] initWithEntityName:@"FoodEntry"];
[fetchRequest setIncludesPropertyValues:NO];
NSError *error;
NSArray *fetchedObjects = [context executeFetchRequest:fetchRequest error:&error];
for (NSManagedObject *object in fetchedObjects)
{
[context deleteObject:object];
}
error = nil;
[context save:&error];
顺便说一下,检查 executeFetchRequest:
和 save:
return 是否有错误总是值得的。
下面是我为 tableView "reset" 按钮实现的方法。我已验证实体中的条目已被删除,但删除后未调用我的 controllerWillChangeContent。有没有办法手动调用这个方法?
CoreDataStack *coreDataStack = [[CoreDataStack alloc]init];
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] initWithEntityName:@"FoodEntry"];
[fetchRequest setIncludesPropertyValues:NO];
NSError *error;
NSArray *fetchedObjects = [coreDataStack.managedObjectContext executeFetchRequest:fetchRequest error:&error];
for (NSManagedObject *object in fetchedObjects)
{
[coreDataStack.managedObjectContext deleteObject:object];
}
error = nil;
[coreDataStack.managedObjectContext save:&error];
编辑:FRC 代码
- (NSFetchedResultsController *)fetchedResultsController {
if (_fetchedResultsController != nil) {
return _fetchedResultsController;
}
CoreDataStack *coreDataStack = [CoreDataStack defaultStack];
NSFetchRequest *fetchRequest = [self entryListFetchRequest];
_fetchedResultsController = [[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest managedObjectContext:coreDataStack.managedObjectContext sectionNameKeyPath:nil cacheName:nil];
_fetchedResultsController.delegate = self;
return _fetchedResultsController;
}
无法确定,因为不了解 CoreDataStack
的内部结构,但看起来您正在使用两个不同的堆栈:请注意您的代码使用:
CoreDataStack *coreDataStack = [[CoreDataStack alloc] init];
而 FRC 使用:
CoreDataStack *coreDataStack = [CoreDataStack defaultStack];
您可以更改您的代码以使用与 FRC 相同的代码(defaultStack
可能是一个单例,并且将 return 调用时使用相同的堆栈)。但是如果FRC和delete代码在同一个class,那么直接使用FRC的managedObjectContext:
NSManagedObjectContext *context = self.fetchedResultsController.managedObjectContext
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] initWithEntityName:@"FoodEntry"];
[fetchRequest setIncludesPropertyValues:NO];
NSError *error;
NSArray *fetchedObjects = [context executeFetchRequest:fetchRequest error:&error];
for (NSManagedObject *object in fetchedObjects)
{
[context deleteObject:object];
}
error = nil;
[context save:&error];
顺便说一下,检查 executeFetchRequest:
和 save:
return 是否有错误总是值得的。