I use a private managed object context to create some new objects in persistent storage, and then after saving the private MOC, merging them into the main MOC using mergeChangesFromContextDidSaveNotification . This works great and updates the user interface as needed, and NSManagedObjectContextWillSaveNotification is NOT used here for mainMOC here.
Then I make some changes to mainMOC using the user interface and listen to NSManagedObjectContextWillSaveNotification . The notification is published, but it contains not only the changes I made, but also objects that were merged with PrivateMOC using mergeChangesFromContextDidSaveNotification .
Is there a way to ignore changes that have been merged from another context into mainContext with subsequent contextDidChange notifications?
Here is the setup:
- (void) loadData { privateContext = [[NSManagedObjectContext alloc] initWithConcurrencyType: NSPrivateQueueConcurrencyType]; privateContext.persistentStoreCoordinator = self.mainContext.persistentStoreCoordinator; [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(contextWillSave:) name:NSManagedObjectContextWillSaveNotification object: self.mainContext]; NSManagedObject *object = [NSEntityDescription insertNewObjectForEntityForName:record.recordType inManagedObjectContext: self.privateContext]; // fill in object if ([self.privateContext hasChanges]) { [self savePrivateContextAndMergeWithMainContext: self.privateContext]; } } - (void) savePrivateContextAndMergeWithMainContext: (NSManagedObjectContext *) privateContext { [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(privateContextDidChange:) name:NSManagedObjectContextDidSaveNotification object:privateContext]; __block NSError *error = nil; [privateContext performBlockAndWait:^{ NSLog(@"PrivateContext saved"); [privateContext save:&error]; }]; [[NSNotificationCenter defaultCenter] removeObserver:self name:NSManagedObjectContextDidSaveNotification object:privateContext]; if (error) { NSLog(@"error = %@", error); } } - (void) privateContextDidChange: (NSNotification *) notification{ [self.mainContext performBlockAndWait:^{ NSLog(@"merged into mainContext"); [self.mainContext mergeChangesFromContextDidSaveNotification:notification]; }]; }
This works fine, and saving the private context and merging with mainContext does not trigger a contextWillSave notification. But when editing data from the user interface (on the main MOC), a notification is triggered and includes data that was previously saved using a private MOC.
Hope this is clear. Let me know if I should include anything else.
- UPDATE -
The problem seems to be related to the removal of objects from the private context. After removing from the private context and calling mergeChangesFromContextDidSaveNotification basically MOC, setMoc deletedObjects still shows the deleted object. This does not happen with inserts or updates in a private context. Is it documented anywhere? What could be the workaround?
ios core-data nsmanagedobject nsmanagedobjectcontext
Zs
source share