NSManagedObjectController not merging in changes from addingManagedObjectContext
I am trying to merge in changes in an addingManagedObjectContext into my main managedObjectContext by calling mergeChangesFromContextDidSaveNotification using an NSFetchedResultsController with CoreData. Like this:
- (void)addGoalViewController:(AddGoalViewController *)controller didFinishWithSave:(BOOL)save {
if (save) {
NSNotificationCenter *dnc = [NSNotificationCenter defaultCenter];
[dnc addObserver:self
selector:@selector(addControllerContextDidSave:)
name:NSManagedObjectContextDidSaveNotification
object:addingManagedObjectContext];
NSLog(@"Before save - addingManagedObjectContext - Number of registered objects: %d", [[addingManagedObjectContext registeredObjects] count]);
NSLog(@"Before save - addingManagedObjectContext - Number of inserted objects: %d", [[addingManagedObjectContext insertedObjects] count]);
NSLog(@"Before save - addingManagedObjectContext - Number of updated objects: %d", [[addingManagedObjectContext updatedObjects] count]);
NSLog(@"Before save - addingManagedObjectContext - Number of deleted objects: %d", [[addingManagedObjectContext deletedObjects] count]);
// Save the new object to the adding managed object context
NSError *error = nil;
if ([addingManagedObjectContext hasChanges] && [addingManagedObjectContext save:&error] == NO) {
// Update to handle the error appropriately.
NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
exit(-1); // Fail
}
NSLog(@"After save - addingManagedObjectContext - Number of registered objects: %d", [[addingManagedObjectContext registeredObjects] count]);
NSLog(@"After save - addin开发者_运维百科gManagedObjectContext - Number of inserted objects: %d", [[addingManagedObjectContext insertedObjects] count]);
NSLog(@"After save - addingManagedObjectContext - Number of updated objects: %d", [[addingManagedObjectContext updatedObjects] count]);
NSLog(@"After save - addingManagedObjectContext - Number of deleted objects: %d", [[addingManagedObjectContext deletedObjects] count]);
// Remove the adding context observer
[dnc removeObserver:self name:NSManagedObjectContextDidSaveNotification object:addingManagedObjectContext];
}
// Reset the adding context back to its default state
[addingManagedObjectContext reset];
// Release the adding managed object context.
self.addingManagedObjectContext = nil;
// Dismiss the add goal view to return to the main list
[self dismissModalViewControllerAnimated:YES];
}
I am following the CoreDataBooks example When I try to merge the changes in though it isn't working. Here is my merge code:
- (void)addControllerContextDidSave:(NSNotification *)saveNotification {
NSManagedObjectContext* context = [fetchedResultsController managedObjectContext];
// Merging changes causes the fetched results controller to update its results
NSLog(@"Before merge - managedObjectContext - Number of registered objects: %d", [[context registeredObjects] count]);
NSLog(@"Before merge - managedObjectContext - Number of inserted objects: %d", [[context insertedObjects] count]);
NSLog(@"Before merge - managedObjectContext - Number of updated objects: %d", [[context updatedObjects] count]);
NSLog(@"Before merge - managedObjectContext - Number of deleted objects: %d", [[context deletedObjects] count]);
[context mergeChangesFromContextDidSaveNotification:saveNotification];
NSLog(@"After merge - managedObjectContext - Number of registered objects: %d", [[context registeredObjects] count]);
NSLog(@"After merge - managedObjectContext - Number of inserted objects: %d", [[context insertedObjects] count]);
NSLog(@"After merge - managedObjectContext - Number of updated objects: %d", [[context updatedObjects] count]);
NSLog(@"After merge - managedObjectContext - Number of deleted objects: %d", [[context deletedObjects] count]);
}
Unfortunately, it doesn't seem to be adding the object to my main managed object context, it adds to to my addingManagedObjectContext but after the merge it should merge them into the main managed object context. However, the number of registered objects in my main managed object context remains at 0. See the 1 registered object in the addingManagedObjectContext after save? That should be in my main managedObjectContext because I merged it, but it isn't. It is sticking in my addingManagedObjectContext instead.
I have looked at the Apple Class Reference documents and from what I have read it seems like the
Before save - addingManagedObjectContext - Number of registered objects: 1
Before save - addingManagedObjectContext - Number of inserted objects: 1
Before save - addingManagedObjectContext - Number of updated objects: 1
Before save - addingManagedObjectContext - Number of deleted objects: 0
Before merge - managedObjectContext - Number of registered objects: 0
Before merge - managedObjectContext - Number of inserted objects: 0
Before merge - managedObjectContext - Number of updated objects: 0
Before merge - managedObjectContext - Number of deleted objects: 0
After merge - managedObjectContext - Number of registered objects: 0
After merge - managedObjectContext - Number of inserted objects: 0
After merge - managedObjectContext - Number of updated objects: 0
After merge - managedObjectContext - Number of deleted objects: 0
After save - addingManagedObjectContext - Number of registered objects: 1
After save - addingManagedObjectContext - Number of inserted objects: 0
After save - addingManagedObjectContext - Number of updated objects: 0
After save - addingManagedObjectContext - Number of deleted objects: 0
As you can see from this output it is adding the object to the addingManagedObjectContext but it is not merging the object into to main managedObjectContext. Instead, the object is remaining in the addingManagedObjectContext. From the Apple Class Reference it seems like this should not be happening. Instead it should merge the object into the managedObjectContext. And there doesn't seem to be any way to check if mergeChangesFromContextDidSaveNotification errors.
It's my understanding that the NSFetchedResultsController should update and merge data in the context automatically. If, prior to saving your second context, your objects are in the Fetched Result Controller's context, it should be watching for those updates. You can check this out yourself by setting the delegate on the controller and implementing the method:
- (void)controllerDidChangeContent:(NSFetchedResultsController *)controller
According to Apple's docs:
Notifies the receiver that the fetched results controller has completed processing of one or more changes due to an add, remove, move, or update.
For more details: NSFetchedResultsControllerDelegate
精彩评论