Cloudkit Sync With CoreData Issue with CKModifyOpe

2019-09-08 07:59发布

Am trying to sync cloudkit and coredata. Have two tables as:

  1. Parent
  2. Child (Child has a CKReference to Parent. i.e Backward reference from child to parent.)

Now according to apple this are the steps we must follow.

  1. Fetch local changes - Done by maintaining a update variable with every record. say 3-delete, 1-create and 2-update.
  2. Upload the local changes to cloud - Here I use CKModifyRecordsOperation and provide the inserted records as one with update value 1 or 3 and 2 as deleted records. (Atomic to avoid inconsistency)
  3. Correct the conflicts if any (So here records with greater modification date are chosen and conflicts are resolved.)
  4. Fetch server changes (Here any change that are made to server from last change token are fetched with CKFetchChangesOperation)
  5. Apply server changes to local()

Now say I have 2 devices and have already synced them with following data

  1. Parent-1
  2. P1-Child1 (references Parent-1)

Now in 1 device I delete Parent-1 and P1-Child1 and let it sync to cloud. On cloudkit dashboard i verify that the Parent and child both have been successfully deleted.

In Device 2, Now I add P1-Child2 another child to previous parent. Consider the above steps

  1. LocalChanges :- (P1-Child2)
  2. Upload to cloud :- (P1-Child2)
  3. Conflicts :- None
  4. Fetch changes from cloud :(Inserted :P1-Child2, Deleted:Parent-1,P1-Child1)
  5. Apply this to local.

P1-Child2 is saved successfully on Cloud without a parent. So now am left with a child record with no parent.

Can you guys help me in figuring out the right method to solve this. I thought if apple could have given an error on CKModifyOperation as mentioned in its doc then I could know that a parent record does not exist and I can re-save or upload a parent record along with the child.

0条回答
登录 后发表回答