Serious Application Error in Core Data with fetchedResultsContainer

我是研究僧i 提交于 2019-12-03 22:37:35
Christoph

Well, this is probably partly (or fully) user error. The problem was that, in the view in which I add a new item, I had put [self.tableView reloadData] inside the viewWillAppear method. Commenting that out did not updated the table cells, but prevented the crash.

I then went ahead and sent reloadRowsAtIndexPaths:withRowAnimation: to the table view to manually reload the few cells that needed it.

I am glad that's finally over!

The default behavior of the fetched results controller is to create one section for each first letter of the sectionNameKeyPath. You shouldn't be getting one section per item unless every item starts with a different letter.

If you want to customize the section name behavior, subclass NSFetchedResultsController and override sectionIndexTitleForSectionName: and sectionIndexTitles. See the NSFetchedResultsController docs for details.

I thought this was my issue. I had got the same warning message, but my solution was VERY different.

You can get this error if you haven't properly implemented all of the NSFetchedResultsController Delegate methods. I always just copy and paste the 4 methods from Apple for how to implement NSFetchedResultsController.

Unfortunately I had missed one of these :

- (void)controllerWillChangeContent:(NSFetchedResultsController *)controller {
    [self.tableView beginUpdates];
}

- (void)controllerDidChangeContent:(NSFetchedResultsController *)controller {
    [self.tableView endUpdates];
}

MAKE SURE YOU'VE GOT EM, else you'll pull out your hair like me.

I discovered another way to arrive at this same cryptic exception. My transient property - like yours, to extract the first letter - wasn't guarded against a 0-length string (@""). The attempt to get its first character threw an exception and resulted in this Core Data error (and not the exception you would expect to see).

wujuan

Important

NSFetchedResultsController * consultMessageFetchedResultsController = [[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest managedObjectContext:[[[WYCoreDataStorage shareStore] coreDataHelper] mainContext] sectionNameKeyPath:nil cacheName:nil];

If you are using a cache, you must call deleteCacheWithName: before changing any of the fetch request, its predicate, or its sort descriptors. You must not reuse the same fetched results controller for multiple queries unless you set the cacheName to nil.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!