开发者

Core Data: updating a managed object doesn't call the NSFetchedResultsControllerDelegate

I have an issue with my table view not updating when I programmatically change some existing managed object property. I would expect my NSFetchedResultsControllerDelegate to be called in such case, but it isn't.

Apple doc says:

An instance of NSFetchedResultsController uses methods in this protocol to notify its delegate that the controller’s fetch results have been changed due to an add, remove, move, or update operations.

In more details, my managed object as an int property called status that takes values from an enum. My table view only display those objects which have their status equal to upToDate (one of the enum's v开发者_如何学编程alues). To do so, my FetchedResultController has a predicate that tests for that status:

predicate = [NSPredicate predicateWithFormat:@"doc.status == %d", upToDate];

It works fine, but updates are not reflected when I change an out-of-date object back to status upToDate. My code is modified from Apple's template.

Here is what I tried:

myObject.status = [NSNumber numberWithInt:upToDate];
[managedObjectContext save:nil];
[managedObjectContext processPendingChanges];
[myTableView reloadData];

And finally, I managed to have my table view update with a call to:

[fetchedResultsController performFetch:&error];
[myTableView reloadData];

But even then, none of my NSFetchedResultsControllerDelegate methods get called, in contradiction with Apple's doc. Additionally, no smooth animation happen.

I expected controllerWillChangeContent:, controller:didChangeObject:atIndexPath:forChangeType:newIndexPath:, and controllerDidChangeContent: to be called.

I suppose I missed something, possibly something obvious, but I can't see what. Does performFetch: need to be called every time some managed object potentially changes? When does the NSFetchedResultsControllerD really call its delegate?

Thanks.


If the FRC delegate methods are never being called regardless of the type of change, then the most likely explanation is that the delegate is not properly configured. It could be caused by:

  1. The delegate is simple not set
  2. The delegate class does not declare the NSFetchedResultsControllerDelegate protocol.
  3. You make the changes in another context without merging.

I would suggest capturing the error return of the save operation to make sure nothing is happening with the save.

The FRC should be notified of any changes to its context regardless of whether they have been saved or not. The FRC should then notify its delegate.


I have had this just happen with a initWithFetchRequest:managedObjectContext:sectionNameKeyPath:cacheName: where then sectionNameKeyPath is non-nil. If I make sure that sectionNameKeyPath is nil then I get the updates like I would expect.

I am still investigating why this is happening

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜