NSFetchedResultsControllerDelegateのセル削除ではまった
題のとおり、NSFetchedResultsControllerDelegateでのdeleteで1h程はまりました。
40秒位で読める程度の中身のなさです。
【結論】
didChangeObjectの引数indexPathとnewIndexPathを書き間違えていた事。
単純かつエラーにならないので気づきにくかった。
【エラー内容】
CoreData: error: Serious application error. Exception was caught during Core Data change processing. This is usually a bug within an observer of NSManagedObjectContextObjectsDidChangeNotification
【で】
サンプルと何も変わらないけれど一応コードです。
case文、NSFetchedResultsChangeDeleteの時にindexPathではなくnewIndexPathを入れてました。
newIndexPathは削除処理の時はnilになるので、削除できず落ちます。
- (void)controller:(NSFetchedResultsController *)controller didChangeObject:(id)anObject atIndexPath:(NSIndexPath *)indexPath forChangeType:(NSFetchedResultsChangeType)type newIndexPath:(NSIndexPath *)newIndexPath { UITableView *tableView = self.tableView; switch(type) { case NSFetchedResultsChangeInsert: [tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:newIndexPath] withRowAnimation:UITableViewRowAnimationFade]; break; case NSFetchedResultsChangeDelete: // 下の行のindexPathをnewIndexPathに間違えていた。delete時はnilがくるので落ちる。 [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade]; [tableView reloadData]; break; case NSFetchedResultsChangeUpdate: [self configureCell:[tableView cellForRowAtIndexPath:indexPath] atIndexPath:indexPath]; break; case NSFetchedResultsChangeMove: [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade]; [tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:newIndexPath] withRowAnimation:UITableViewRowAnimationFade]; break; } }
あまりに単純ミスなので同じ事する人はいないかもですが一応書きました。
解決のヒントはMaster-Detail-Applicationのデフォルトの処理でindexPathがnilじゃないじゃん!
ってところからでした。
その他の良くありそうなエラー
NSFetchedResultsControllerの検索条件が違う時にキャッシュが同名の時
NSFetchedResultsController performFetchでクラッシュ