Search code examples
iosobjective-ccore-datansmanagedobjectcontext

Identifying which fields have changed before CoreData saves


//set up notifications

[[NSNotificationCenter defaultCenter]
 addObserver:self
 selector:@selector(dataChanged:)
 name:NSManagedObjectContextDidSaveNotification
 object:context];    

//later

- (void)dataChanged:(NSNotification *)notification{
  NSDictionary *info = notification.userInfo;
  NSSet *insertedObjects = [info objectForKey:NSInsertedObjectsKey];
  NSSet *deletedObjects = [info objectForKey:NSDeletedObjectsKey];
  NSSet *updatedObjects = [info objectForKey:NSUpdatedObjectsKey];

Is there anyway to determine from the updatedObjects which fields were actually changed?

thanks, Michael


Solution

  • The following should do the trick, but you will need to use NSManagedObjectContextWillSaveNotification and access your updated objects through the same NSManagedObjectContext used to save the objects.

    for(NSManagedObject *obj in updatedObjects){
    
       NSDictionary *changes = [obj changedValues];
       // now process the changes as you need
    
    }
    

    See the discussion in the comments.