Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NSFetchedResultsController delegate methods not firing after mergeChangesFromContextDidSaveNotification

Been browsing through the posts similar to this one but none answered my problem.

Like CoreDataBooks, I use a separate MOC for adding and editing items and observe the proper notifications, like so:

- (void)addControllerContextDidSave:(NSNotification*)saveNotification {

  NSManagedObjectContext *context = [self.fetchedResultsController managedObjectContext];
  // Merging changes causes the fetched results controller to update its results
  [context mergeChangesFromContextDidSaveNotification:saveNotification];
  NSLog(@"merging changes in rootview");
}

When I do a save, it's observed correctly. Debugging the notification shows the right context, and debugging the context shows the just added item. The log message also prints just fine. But nothing happens. None of the delegate methods of the NSFetchedResultsController fire, nor is the table view updated.

I tried no cache or clearing it. I've tried reloadData on the tableView, but nothing. When I quit the app and restart, it shows up fine.

UPDATE: The notification does indeed contain the right context as its object, which contains the correct item, but the userInfo hash has nothing in it:

userInfo = {
  inserted = "{(\n)}";
  updated = "{(\n)}";
}

So it looks like the notification doesn't have any changes in it, despite having the correct context with the changed item.... hmmmm.

like image 394
Christoph Avatar asked Oct 21 '10 04:10

Christoph


1 Answers

And the award for dumbest mistake goes toooooo ... Christoph!!

The reason the notification was not showing anything was that I had forgotten to take out a save on the separate MOC before calling the delegate that set up the notification and then saved again.

Since the MOC was just saved, the second save had no changes and thus the notification stayed empty. Removing the first save, as I should have done much earlier, did fix the problem.

like image 173
Christoph Avatar answered Sep 28 '22 08:09

Christoph