//设置通知
[[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];
有没有从更新对象确定哪些字段实际上改变?
谢谢,
迈克尔
解决方法
以下应该做的诀窍,但您将需要使用NSManagedObjectContextWillSaveNotification,并通过用于保存对象的相同的NSManagedObjectContext访问更新的对象.
for(NSManagedObject *obj in updatedObjects){ NSDictionary *changes = [obj changedValues]; // now process the changes as you need }
参见评论中的讨论.