3
votes

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

  • User updates :- the new updates are lost when child saves the record to parent.

  • User Deletes :- (we just mark it as deleted) the child re-saves the child since it do not get the property marked as deleted.

    Question:

    1. Can child somehow know of the parent updates? Can i refresh the object before i save?

    2. In Parent-Child Setup does child always overrides parent changes without getting merge conflicts?

2

2 Answers

2
votes
  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.

-1
votes

Have a look at this tutorial it may help.

Multiple Context swift tutorial