我有一个 NSFetchedResultsController 作为我的数据源,并且我在我的自定义 UITableViewController 中实现了 NSFetchedResultsControllerDelegate。我正在使用 sectionNameKeyPath 将我的结果集分成多个部分。
在我的一种方法中,我将几个对象添加到上下文中,所有这些对象都在一个新部分中。在我保存对象的那一刻,委托方法被正确调用。事件顺序:
// -controllerWillChangeContent: fires
[self.tableView beginUpdates]; // I do this
// -controller:didChangeSection:atIndex:forChangeType: fires for section insert
[self.tableView insertSections:[NSIndexSet indexSetWithIndex:sectionIndex]];
// -controller:didChangeObject:atIndexPath:forChangeType:newIndexPath fires many times
[self.tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:newIndexPath]
withRowAnimation:UITavleViewRowAnimationFade]; // for each cell
// -controllerDidChangeContent: fires after all of the inserts
[self.tableView endUpdates]; // <--- Where things go terribly wrong!!!
在最后一次调用“endUpdates”时,应用程序总是崩溃:
Serious application error. Exception was caught during Core Data change processing:
[NSCFArray objectAtIndex:]: index (5) beyond bounds (1) with userInfo (null)
似乎表格更新在某种程度上与 NSFetchedResultsController 数据不同步,并且事情发生了变化。我正在关注 NSFetchedResultsControllerDelegate 上的文档,但它不起作用。正确的方法是什么?
更新:我创建了一个显示此错误的测试项目。您可以在以下位置下载它:NSBoom.zip