データソースとしてNSFetchedResultsControllerを使用しており、カスタムUITableViewControllerにNSFetchedResultsControllerDelegateを実装しています。sectionNameKeyPathを使用して、結果セットを複数のセクションに分割しています。
私のメソッドの1つでは、コンテキストにいくつかのオブジェクトを追加しています。これらはすべて新しいセクションにあります。オブジェクトを保存した時点で、デリゲートメソッドが適切に呼び出されています。イベントの順序:
// -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からダウンロードできます。