This system uses the multi-threaded Core Data configuration of two contexts, shared persistent store.
I have an MOC created on the main thread of type NSMainQueueConcurrencyType
(MOC A). There is an observer registered for NSManagedObjectContextObjectsDidChangeNotification
s for MOC A.
Elsewhere, on a background thread in a queue, a background MOC (MOC B) of type NSPrivateQueueConcurrencyType
and sharing the same PersistentStoreCoordinator
as MOC A is created. After some operations, a save
is called on MOC B.
An object is observing NSManagedObjectContextDidSaveNotification
s on the background MOC B. Upon notification, it calls on the main thread a mergeChangesFromContextDidSaveNotification
on MOC A.
Newly inserted and updated objects in MOC B do cause change notifications for observers on MOC A, but deletions in MOC B are not causing these change notifications. A fetch on MOC A does reflect that the deletions in MOC B were propagated to MOC A, but the issue is that there is no change notification. This is worrisome because views and controllers may think deleted objects are still valid.
Any guesses on why this could be happening?
Creation of MOC A
_managedObjectContext = [[NSManagedObjectContext alloc] initWithConcurrencyType:NSMainQueueConcurrencyType];`
Creation of MOC B
NSPersistentStoreCoordinator *psc = [[NSManagedObject managedObjectContext] persistentStoreCoordinator]; dispatch_async(_backgroundQueue, ^{ // create second MOC NSManagedObjectContext *bgMoc = [[NSManagedObjectContext alloc] initWithConcurrencyType:NSPrivateQueueConcurrencyType]; bgMoc.persistentStoreCoordinator = psc; bgMoc.undoManager = nil; [self registerForNotificationWithManagedObjectContext:bgMoc]; });
Notification Handling for NSManagedObjectContextDidSaveNotification
- (void)managedObjectContextObjectsDidSaveNotification:(NSNotification *)notification { SEL selector = @selector(mergeChangesFromContextDidSaveNotification:); [[NSManagedObject managedObjectContext] performSelectorOnMainThread:selector withObject:notification waitUntilDone:YES]; }
EDIT: I used the recommended SO answer for merging two MOCs from here: https://stackoverflow.com/a/6959868/1505750