I have a NSFetchedResultsController which displays data in a UITableView. I'm using the boiler plate code provided by Xcode when choosing to create a Core Data project. I added the following predicate to the NSFetchRequest the NSFetchedResultsController uses (before NSFetchedResultsController is initialized):
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"deleted == NO"];
[fetchRequest setPredicate:predicate];
Now in another location of my app, I set the deleted property like so (pseudo-code):
myManagedObject.deleted = YES
saveDataContext
When I return to the TableViewController, it still shows this "deleted" row.
When I try to reload the table view, nothing happens.
When I try to reload the fetchedResultsController using performFetch
, it says:
'FATAL ERROR: The persistent cache of section information does not match the current configuration. You have illegally mutated the NSFetchedResultsController's fetch request, its predicate, or its sort descriptor without either disabling caching or using +deleteCacheWithName:'
If I remove the caching, in the init method, call performFetch
, then call [myTable reloadData]
it works.
Isn't there a simpler way to refresh the data? Preferably one that allows you to use the caching feature of NSFetchedResultsController?
As far as I know, the only place I am modifying the fetch request, predicate, or sort descriptor is in the same method that allocs and inits the NSFetchedResultsController, so it seems that the error message that it displays is incorrect.
Update: Now that I understand the NSFetchedResultsController
a bit better, I understand that it won't remove rows for you automatically, and it's the controller:didChangeObject:atIndexPath:forChangeType:nowIndexPath:
method that is primarily responsible for deleting rows. I had this method implemented, since I used Apple's template for my project.
However, in my case I'm not actually deleting an item, I'm just updating a property (deleted
) to specify that the item should no longer exist in the list. This means that the controller:didChangeObject:atIndexPath:forChangeType:nowIndexPath:
method's change type is NSFetchedResultsChangeUpdate
and not NSFetchedResultsChangeDelete
. I updated the code to something such as:
case NSFetchedResultsChangeUpdate: {
MyObj *obj = (MyObj *)anObject;
if (obj.deletedValue) { // NOTE: deletedValue returns the deleted property as BOOL
[tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
} else {
[self configureCell:[tableView cellForRowAtIndexPath:indexPath] atIndexPath:indexPath];
break;
}
}
The problem with this is that I get the error message:
Invalid update: invalid number of rows in section 0. The number of rows contained in an existing section after the update (3) must be equal to the number of rows contained in that section before the update (3), plus or minus the number of rows inserted or deleted from that section (1 inserted, 2 deleted). with userInfo (null)
Basically it's complaining that the number of objects in the NSFetchedResultsController and the number of cells in the table aren't synced.
Hopefully this makes it clearer:
// DB Items (Name, Deleted):
[Foo, NO]
[Bar, NO]
[Baz, NO]
// mark one as deleted
Objects[1].deletedValue = YES;
// DB items now look like so:
[Foo, NO]
[Bar, YES]
[Baz, NO]
Even though NSFetchedResultsController's NSFetchRequest's NSPredicate is set to deleted == NO
, the NSFetchedResultsController still sees 3 items instead of 2.
How can I solve this issue? Would I need to refresh the NSFetchedReultsController somehow? What else could be the problem?