我有一个NSFetchedResultsController作为我的数据源,并且我在自定义的UITableViewController中实现了NSFetchedResultsControllerDelegate。我使用sectionNameKeyPath将我的结果集分成多个部分。你如何处理与NSFetchedResultsController的部分插入?
在我的一个方法中,我将两个对象添加到上下文中,所有这些对象都位于新节中。在我保存对象的那一刻,委托方法被正确调用。事件的顺序:
// -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
谢谢!这一观察(双重更新)让我想出了一个我可以使用的解决方法。 – 2009-10-14 12:36:58