在CoreData保存之前确定哪些字段已更改

时间:2011-05-12 10:11:09

标签: ios objective-c core-data nsmanagedobjectcontext

//设置通知

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

//后

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

无论如何要从updatedObjects确定实际更改了哪些字段?

感谢, 迈克尔

1 个答案:

答案 0 :(得分:18)

以下应该可以解决这个问题,但您需要使用NSManagedObjectContextWillSaveNotification并通过用于保存对象的相同NSManagedObjectContext访问更新的对象。

for(NSManagedObject *obj in updatedObjects){

   NSDictionary *changes = [obj changedValues];
   // now process the changes as you need

}

请参阅评论中的讨论。