views:

37

answers:

1

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 values). 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.

A: 

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.

TechZen
1. `aFetchedResultsController.delegate = self;` is present2. Double checked.3. I have only one context ever, which is passed along from the app delegate.As I wrote, it's basically the vanilla Apple code. Any other idea what else to check?And of course I capture the error from save. I just simplified my code snippet for this question.
Jean-Denis Muys