3

I have an NSFetchedResultsController on the main thread. Also from the main thread, I asynchronously send out a network request for JSON. When that JSON string returns, I start an NSOperation that inits a new (background) NSManagedObjectContext, parses the JSON string, creates/updates NSManagedObject's, and saves them in the context. The background context has the same persistentStore as the main context. With this, I have 2 questions:

  1. I thought that any saves to the persistent store from any context (on any thread) would notify the main NSFetchedResultsController that there are changes, but so far it doesn't pick up any changes. Is there something I should do to notify the main thread's NSFetchedResultsController that there were external save's so that the tableView updates accordingly?

  2. So, on the main thread, I subscribe to NSManagedObjectContextWillSaveNotification and correctly see when all contexts (including those existing entirely on a separate thread) perform a save operation. The apple docs say that the notification.userInfo should have a dictionary of 3 arrays, one array for each of the "updated, deleted, and inserted" model objects on the background thread. However, the userInfo is always nil for me. Any ideas what I'm doing wrong?

Subscribing to the NSManagedObjectContextWillSaveNotification in AppDelegate:

[[NSNotificationCenter defaultCenter] addObserver:self 
                                         selector:@selector(managedObjectContextDidSave:)
                                             name:NSManagedObjectContextWillSaveNotification 
                                           object:nil];

And the method for when contexts are saved in AppDelegate:

- (void)managedObjectContextDidSave:(NSNotification *)notification {
    DLog(@"notification: %@", notification); //not nil
    DLog(@"notification user info: %@", notification.userInfo); // always nil... why??

    NSManagedObjectContext *theContext = notification.object;
    if(theContext != context) {
        DLog(@"---- SAVED ON ANOTHER CONTEXT");

        // should I notify NSFetchedResultsController that there were context saves on background threads?
        // how can I merge contexts if userInfo is nil?
    }
}

I'd also like to know the best practices in dealing with multiple threads (with separate NSManagedObjectContexts) and Core Data.

johngraham
  • 6,461
  • 2
  • 16
  • 22

1 Answers1

5

You observe the wrong notification: the name of the notification you need to observe is NSManagedObjectContextDidSaveNotification (not NSManagedObjectContextWillSaveNotification).

Nicolas Bachschmidt
  • 6,475
  • 2
  • 26
  • 36
  • great! userInfo is now filled with the correct dictionaries. what about notifying the `NSFetchedResultsController` about the changes - do you recommend I merge these changes to the main context or is there an easier way to update the `NSFetchedResultsController`? – johngraham Dec 22 '11 at 19:50
  • 3
    The controller relies on the main context, so the correct way (indeed the only way) to do this is to merge the changes into that context. – paulbailey Dec 22 '11 at 19:50
  • 2
    Yes, you should tell the main context to merge the changes using `-[NSManagedObjectContext mergeChangesFromContextDidSaveNotification:]`. If the `NSFetchedResultController` has a delegate, it will automatically refresh it contents and notify the changes to its delegate. Otherwise, you can reload the contents by sending `performFetch:`. – Nicolas Bachschmidt Dec 22 '11 at 20:30