Am trying to sync cloudkit and coredata. Have two tables as:
- Parent
- 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.
- Fetch local changes - Done by maintaining a update variable with every record. say 3-delete, 1-create and 2-update.
- 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)
- Correct the conflicts if any (So here records with greater modification date are chosen and conflicts are resolved.)
- Fetch server changes (Here any change that are made to server from last change token are fetched with CKFetchChangesOperation)
- Apply server changes to local()
Now say I have 2 devices and have already synced them with following data
- Parent-1
- 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
- LocalChanges :- (P1-Child2)
- Upload to cloud :- (P1-Child2)
- Conflicts :- None
- Fetch changes from cloud :(Inserted :P1-Child2, Deleted:Parent-1,P1-Child1)
- 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.