user3519594
user3519594

Reputation: 397

CoreData Parent Child Context Conflict Management

Core data application, that Syncs user data from CloudKit.

We have two core data context

  1. Main Context :- UI (Insert, Update, Delete from User )
  2. Child Context :- That fetches changes from cloud kit and once done saves the changes to Main Context.

Problems:

Since its a parent-child setup the changes from parent do not get reflected in child. So while the child is syncing changes to CloudKit if the parent context get updated, then child is not aware of that changes.

Say child context is trying to upload a record A to CloudKit meanwhile if user modifies the same record as below

Upvotes: 3

Views: 619

Answers (2)

malhal
malhal

Reputation: 30811

  1. Set the context's automaticallyMergesChangesFromParent property.
  2. Choose an appropriate merge policy.

FYI Parent/Child contexts are not for this they are for create/update screens, where changes made by the user can be cancelled and discarded leaving the main context alone. For network sync you should be using a background context associated with the persistent store. See NSPersistentContainer newBackgroundContext.

Upvotes: 2

Ganesh Amrule
Ganesh Amrule

Reputation: 427

Have a look at this tutorial it may help.

Multiple Context swift tutorial

Upvotes: -1

Related Questions