使用NSManagedObjectContextWillSaveNotification标识已更改对象的旧值和新值 [英] Identifying old and new values on changed objects with NSManagedObjectContextWillSaveNotification
问题描述
我正在跟踪核心数据上下文中对象的更改,跟踪已更改的属性的名称以及旧值和新值。
ve注册为 NSManagedObjectContextWillSaveNotification
在保存即将发生时接收通知,并可以从上下文中拉出插入/更新/删除的对象...然后我可以看到
但是,我在检索旧值时遇到困难...
例如,我有一个跟踪位置的对象,因此其中一个更改返回: po [obj changedValues]
{
originX = 260;
originY = 180;
}
这给出了对象上已更改的属性的新值。为了尝试获得旧的值,我然后使用 changedValuesForCurrentEvent
,根据文档应该返回
一个包含自上次发布以来发生变化的持久
属性的键和旧值的字典
NSManagedObjectContextObjectsDidChangeNotification
但是,当我尝试这个,它回来为空...:
po [obj changedValuesForCurrentEvent]
{
}
如何捕获旧值和新值?
NSManagedObjectContextObjectsDidChangeNotification
在您每次更改受管对象上的值时调用,即使您尚未保存更改。 NSManagedObjectContextWillSaveNotification
稍后调用时保存。所以顺序是:
- 您更改了一些属性 - >
NSManagedObjectContextObjectsDidChangeNotification
,您可以使用changedValuesForCurrentEvent
查看已更改的内容。 - 稍后,您保存更改。已发布
NSManagedObjectContextWillSaveNotification
。您可以调用changedValuesForCurrentEvent
,但它没有帮助,因为它返回自上次更改通知以来的更改 。自上次更改通知后没有任何更改。如果有,你会收到另一个。
如果您希望旧的值和您希望在发布即时保存通知时获取它们,您有以下几个选项:
- 监听
NSManagedObjectContextObjectsDidChangeNotification
。缓存一些集合对象(可能NSDictionary
)中的更改的信息。然后,当NSManagedObjectContextWillSaveNotification
发生时,查找这些更改,处理它们,并清除更改缓存。 OR ... - 当您获得
NSManagedObjectContextWillSaveNotification
时,创建第二个本地管理对象上下文。由于这是将保存通知,您仍然可以获取旧的值。因此,获取保存的每个对象,并比较之前和之后的值,看看有什么不同。
I am trying to track changes to objects in a core data context, tracking the name of properties that have changed along with the old and new values.
I've registered for NSManagedObjectContextWillSaveNotification
to receive a notification when a save is about to occur, and can pull out the inserted/updated/deleted objects from the context... I can then see the changed values using .changedValues.
However, I am having difficulties retrieving the old values...
As an example, I have an object that tracks a position, and so one of the changes comes back with:
po [obj changedValues]
{
originX = 260;
originY = 180;
}
This gives me the new values for the properties that have changed on the object. To try and get the old values, I'm then using changedValuesForCurrentEvent
, which according to the docs should return
"a dictionary containing the keys and old values of persistent properties that have changed since the last posting of
NSManagedObjectContextObjectsDidChangeNotification
"
However, when I try this, it is coming back empty...:
po [obj changedValuesForCurrentEvent]
{
}
How can I capture the old and new values?
You're mixing up your notifications. NSManagedObjectContextObjectsDidChangeNotification
gets called any time you change values on a managed object, even though you haven't saved changes yet. NSManagedObjectContextWillSaveNotification
gets called later on when you save. So the sequence is:
- You change some attributes -->
NSManagedObjectContextObjectsDidChangeNotification
is posted, and you can usechangedValuesForCurrentEvent
to see what changed. - Later, you save changes.
NSManagedObjectContextWillSaveNotification
is posted. You can callchangedValuesForCurrentEvent
, but it's not helpful because it returns changes since the last did-change notification. There are no changes since the last did-change notification. If there were, you would have received another one. That method is documented to be useful on a did-change notification, not on a will-save notification.
If you want the old values and you want to get them when the will-save notification is posted, you have a couple of options:
- Listen for
NSManagedObjectContextObjectsDidChangeNotification
. Cache information about changes in some collection object (probablyNSDictionary
). Then whenNSManagedObjectContextWillSaveNotification
happens, look up those changes, process them, and clear the change cache. OR... - When you get
NSManagedObjectContextWillSaveNotification
, create a second local managed object context. Since this is a will save notification, you can still fetch the old values. So, fetch each object that's getting saved and compare the before and after values to see what's different.
这篇关于使用NSManagedObjectContextWillSaveNotification标识已更改对象的旧值和新值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!