更新时间:2023-09-20 13:19:34
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:
NSManagedObjectContextObjectsDidChangeNotification
is posted, and you can use changedValuesForCurrentEvent
to see what changed.NSManagedObjectContextWillSaveNotification
is posted. You can call changedValuesForCurrentEvent
, 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:
NSManagedObjectContextObjectsDidChangeNotification
. Cache information about changes in some collection object (probably NSDictionary
). Then when NSManagedObjectContextWillSaveNotification
happens, look up those changes, process them, and clear the change cache. OR...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.