我有一个应用程序使用 Core Data 来驱动带有部分的 UITableView
。它使用所有示例项目使用的标准 NSFetchedResultsControllerDelegate
实现。然而,偶尔但不可靠地,调用 [self.tableView endUpdates]
会抛出以下异常:
CoreData: error: Serious application error. An exception was caught from the delegate of NSFetchedResultsController during a call to -controllerDidChangeContent:. attempt to insert row 3 into section 1, but there are only 3 rows in section 1 after the update with userInfo (null)
在那之后,表格 View 只有当时可见的单元格和其他地方的空白空间(但仍可滚动)。除了重新启动应用程序外,没有什么可以修复它。我确信应用程序会崩溃,除非 NSFetchedResultsController
正在捕获异常。
应用重新启动后,导致回调的数据立即出现。果然,第 1 节中只有 3 行(或任何异常(exception)情况)。 那么,Core Data 到底为什么要告诉我插入一个不存在的行?
- (void)reloadFetch
{
NSError *error = nil;
if (![self.fetchedResultsController performFetch:&error]) {
TULogError(@"Unresolved error %@, %@", error, [error userInfo]);
[[TCCacheController sharedController] clearData];
}
[self.tableView reloadData];
}
- (NSFetchedResultsController *)fetchedResultsController
{
if (_fetchedResultsController == nil) {
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] initWithEntityName:@"FeedItem"];
fetchRequest.fetchBatchSize = 20;
fetchRequest.predicate = [NSPredicate predicateWithFormat:@"city = %@", [TCCity currentCity]];
fetchRequest.sortDescriptors = @[ [NSSortDescriptor sortDescriptorWithKey:@"updatedAt" ascending:NO] ];
fetchRequest.relationshipKeyPathsForPrefetching = @[
@"user",
@"responses",
];
_fetchedResultsController = [[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest
managedObjectContext:[TCCacheController sharedController].mainContext
sectionNameKeyPath:@"day"
cacheName:nil];
_fetchedResultsController.delegate = self;
[self reloadFetch];
}
return _fetchedResultsController;
}
- (void)controllerWillChangeContent:(NSFetchedResultsController *)controller
{
[self.tableView beginUpdates];
}
- (void)controller:(NSFetchedResultsController *)controller
didChangeSection:(id <NSFetchedResultsSectionInfo>)sectionInfo
atIndex:(NSUInteger)sectionIndex
forChangeType:(NSFetchedResultsChangeType)type
{
switch(type) {
case NSFetchedResultsChangeInsert:
[self.tableView insertSections:[NSIndexSet indexSetWithIndex:sectionIndex] withRowAnimation:UITableViewRowAnimationFade];
break;
case NSFetchedResultsChangeDelete:
[self.tableView deleteSections:[NSIndexSet indexSetWithIndex:sectionIndex] withRowAnimation:UITableViewRowAnimationFade];
break;
}
}
- (void)controller:(NSFetchedResultsController *)controller
didChangeObject:(id)anObject
atIndexPath:(NSIndexPath *)indexPath
forChangeType:(NSFetchedResultsChangeType)type
newIndexPath:(NSIndexPath *)newIndexPath
{
switch(type) {
case NSFetchedResultsChangeInsert: {
[self.tableView insertRowsAtIndexPaths:@[newIndexPath] withRowAnimation:UITableViewRowAnimationFade];
break;
} case NSFetchedResultsChangeDelete: {
[self.tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
break;
} case NSFetchedResultsChangeUpdate: {
[self.tableView reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
break;
} case NSFetchedResultsChangeMove: {
[self.tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
[self.tableView insertRowsAtIndexPaths:@[newIndexPath] withRowAnimation:UITableViewRowAnimationFade];
break;
}
}
}
- (void)controllerDidChangeContent:(NSFetchedResultsController *)controller
{
[self.tableView endUpdates];
}
最佳答案
我遇到了类似的问题,但我将自己的自定义部分添加到从 NSFetchedResultsController 返回的结果中。为此,我必须确保在管理返回结果时调整索引路径。我错过了在 didChangeObject 方法中处理索引路径。
我以非常暴力的方式实现了更改索引路径,但在寻找这个问题时,我在这篇 SO 帖子中发现了一个非常有用且更优雅的解决方案:NSFetchedResultsController prepend a row or section (参见 JosephPH 的回答)。
关于ios - 核心数据导致 endUpdates 异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19103712/