Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In NSManagedObjectContextObjectsDidChangeNotification what is the difference between NSUpdatedObjectsKey and NSRefreshedObjectsKey

The documentation says;

NSUpdatedObjectsKey Key for the set of objects that were updated.

NSRefreshedObjectsKey Key for the set of objects that were refreshed but were not dirtied in the scope of this context.

What does this actually mean. I just want to know what has changed, so do I need to watch both these keys?

like image 346
trapper Avatar asked Feb 03 '17 02:02

trapper


1 Answers

NSUpdatedObjectsKey means that a property of the object has changed. However if you set an property to the same values that was already there then it will also appear as NSUpdatedObjectsKey even if nothing really changed. (ie issue.issueId = issue.issueId will cause the object to be "changed").

NSRefreshedObjectsKey means that the object was refetched from the store. If you are using more than one context at the same time then the object may have changed when you weren't looking. So calling refreshObject:mergeChanges: on the context for those objects will cause them to be refetched from the store. This does not mean that they have change - but they may have.

In regards to using them, I am not sure why you would need either. Since NSPersistentContainer, I haven't had any reason to use NSManagedObjectContextDidSaveNotification at all, as NSPersistentContainer manages all of the context merging and updates for me. When I did manage my own core data stack I mostly just passed along the entire notification to mergeChangesFromContextDidSaveNotification:. I never called refreshObject:mergeChanges: as I had other ways of making sure that the contexts remained in sync, so I never had any RefreshedObjects.

I'm not sure if answered your question, I hope that helps.

like image 52
Jon Rose Avatar answered Nov 15 '22 02:11

Jon Rose