Search code examples
cocoacore-datansnotificationcenter

NSManagedObjectContextObjectsDidChangeNotification after insertion


When i add a new NSManagedObject instance to the NSManagedObjectContext like this:

NSManagedObject *newObject = [NSEntityDescription insertNewObjectForEntityForName:@"Bla" inManagedObjectContext:self.managedObjectContext];
[newObject setValue:@"New Object" forKey:@"name"];

i get 1 NSManagedObjectContextObjectsDidChangeNotification with a NSInsertedObjectsKey and as the new value the "name" value "New Object".

I would expect to get:

  • 1 InsertedObjectKey notification with new value of "name" = nil
  • 1 UpdatedObjectKey notification with new value of "name" = "New Object"

I have the feeling this is a feature, but i'm note sure..


Solution

  • Managed object contexts bunch together their changes. You don't get notified for every single value change, but instead periodically at the end of each "lump" of changes.

    If you particularly want a notification for each change separately, in the above code you could call -processPendingChanges in between