标识在CoreData保存之前哪些字段已更改 [英] Identifying which fields have changed before CoreData saves

查看:124
本文介绍了标识在CoreData保存之前哪些字段已更改的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

//设置通知

[[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确定哪些字段实际改变了?

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

感谢,
Michael

thanks, Michael

推荐答案

需要使用NSManagedObjectContextWillSaveNotification,并通过用于保存对象的同一NSManagedObjectContext访问您更新的对象。

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

}

查看评论中的讨论。

这篇关于标识在CoreData保存之前哪些字段已更改的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆