Andrew
Andrew

Reputation: 16051

NSManagedObjectContextDidSaveNotification with numerous core data stores

I have 2 separate data stores in my app, both of which go onto a background thread at the same time. I therefore have this code to set it up:

NSNotificationCenter *notificationCenter = [NSNotificationCenter defaultCenter];
    [notificationCenter addObserver:self.dataStoreManager
                           selector:@selector(mergeChanges:)
                               name:NSManagedObjectContextDidSaveNotification
                             object:nil];
    NSPersistentStoreCoordinator *dataStoreBackgroundPSC = self.dataStoreManager.managedObjectContext.persistentStoreCoordinator;

    [notificationCenter addObserver:[AppDelegate applicationDelegate].coreDataManager
                           selector:@selector(mergeChanges:)
                               name:NSManagedObjectContextDidSaveNotification
                             object:nil];
    NSPersistentStoreCoordinator *journalDataPSC = [AppDelegate applicationDelegate].coreDataManager.persistentStoreCoordinator;

Will this cause problems, as both objects will receive the notification, or will they treat it in a way that won't negatively impact either data store?

EDIT: Ok, it turns out this is, in fact, not good. Alternatives? If i don't save one of them on the background thread, does it still need that notification?

Upvotes: 0

Views: 420

Answers (1)

KHansenSF
KHansenSF

Reputation: 604

I assume that you are looking at the NSManagedObjectContextDidSaveNotification from the different NSManagedObjectContexts associated with each NSPersistentStoreCoordinator.

Specify those as the object: parameter instead of nil in your calls to addObserver.

Upvotes: 1

Related Questions