I read apple developer document for core data merge policy about NSMergeByPropertyStoreTrumpMergePolicy.
https://developer.apple.com/documentation/coredata/nsmergebypropertystoretrumpmergepolicy
In document,
A policy that merges conflicts between the persistent store's version of the object and the current in-memory version by individual property, with the in-memory changes trumping external changes.
The merge occurs by individual property. For properties that have been changed in both the external source and in memory, the external changes trump the in-memory ones.
I can't understand what 'individual property', 'in-memory changes trumping external change' and 'the external changes trump the in-memory ones' means.
I think 'in-memory changes' is current context to save. And 'external change' is persistent store changed by other context before. Is it right?
Thanks for your help.
When you set the merge policy you are asking core-data to lose data that you saved - the only question is what data. Ideally you want to avoid any write conflicts in core data. Write conflicts can happen when you have more than one context writing to the store at the same time. You should create an operation queue to ensure that you don't get conflicts. (see NSPersistentContainer concurrency for saving to core data).
To answer your question: in-memory means the changes that are in a context that you just called save
on. "external change" means what is going on in the store at that moment. Those external changes are often just another context that was writing at the same time from a different thread.
To understand the difference between OverwriteMergePolicy
and MergeByPropertyObjectTrump
you have to know that the context knows what properties have been changed to and what properties that have changed from. It is a conflict when the store has a value for a property that is not the from
value that the context has. There are cases when the value wasn't change in the context but still has a different value from the store. In the overwrite policy those values are also changed, in the by Property policy only the properties that were changed by the context as changed.
So if the store had an object:
property 1: A
property 2: A
property 3: A
and in memory had:
property 1: A -> B // no conflict - regular change
property 2: B -> C // "by property" conflict - store has wrong *from* value
property 3: B // conflict but not changed by the context
For a overwrite policy the object will be saved as it currently is in the context to the store, even property 3 which was not edited by the context
property 1: B
property 2: C
property 3: B
For a by property policy the object will only force changes for properties that it changed. So property 3 will remain A because it was not edited by the context and property 2 will change even though it saw a different from
value than it expected.:
property 1: B
property 2: C
property 3: A
Again, as I said above you really should try to avoid merge conflict entirely and not use merge policy at all.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With