Identifying which fields have changed before CoreData saves

半腔热情 提交于 2019-12-04 16:58:08

问题


//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


回答1:


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.



来源:https://stackoverflow.com/questions/5976455/identifying-which-fields-have-changed-before-coredata-saves

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!