Search code examples
ioscore-dataconcurrencynsmanagedobjectcontextnspersistentstore

Child Parent Context Save Conflicts


How are conflicts resolved when a child NSManagedObjectContext is saved and the changes are pushed to the parent NSManagedObjectContext?

For instance, lets say you are updating NSManagedObject Person by setting the attribute age to 18 on a child context with an NSPrivateQueueConcurrencyType from data from a server. At the same time, the same NSManagedObject Person age attribute is updated to a value of 20 by the user on the parent NSManagedObjectContext with NSMainQueueConcurrencyType.

After both updates a save is called on the child context which propagates to the parent NSManagedObjectContext.

What is the value of age at this point? Does the child NSManagedObjectContext overwrite the parent NSManagedObjectContext? Do NSMergePolicies apply here?


Solution

  • There is no conflict resolution when saving from a child to a parent. The child always changes the parent, regardless of what values the parent has, or whether the parent has unsaved changes itself.

    Furthermore, there is no merge policy or built-in other way to alter this behavior.

    It is possible to write your own merge policy management, but it's a bit tricky. You are probably better off just knowing the rules.