I am contemplating utilizing CloudKit in my app (it does not use Core Information) and have learn as many supplies as I can discover. I have not absolutely grasped it but and have a primary query on CKRecord.Reference
. Does CloudKit assure CKRecord.Reference
worth is at all times legitimate? By legitimate I imply the goal CkRecord
pointed by the CKRecord.Reference
exists within the database.
Let’s think about an instance. Suppose there are two tables: Account
and Transaction
:
Account Desk:
AccountNumber Forex Charge
------------- -------- ----
a1 USD 0.03
Transaction Desk:
TransactionNumber AccountNumber Quantity
----------------- ------------- ------
t1 a1 20
Now suppose consumer does the next:
- Person first deletes account
a1
and its related transactionst1
on machine A. The machine saves the change to cloud. - Then consumer provides a brand new transaction
t2
to accounta1
on machine B, earlier than the machine receives the change made in step 1 from cloud. Sincea1
hasn’t been deleted on machine B, the operation ought to succeed regionally. The machine tries to avoid wasting the change to cloud too.
My questions:
Q1) Will machine B have the ability to save the change in step 2 to cloud?
I hope it could fail, as a result of in any other case it could result in inconsistent knowledge. However I discover the next in CKModifyRecordsOperation
doc (emphasis mine), which means CloudKit permits invalid reference:
Throughout a save operation, CloudKit requires that the goal report of the mum or dad reference, if set, exists within the database or is a part of the identical operation; all different reference fields are exempt from this requirement.
(BTW, I believe the truth that, when utilizing CloudKit, Core Information requires all relations have to be non-compulsory additionally signifies that CloudKit cannot assure relation is at all times legitimate, although I believe that’s primarily a problem on shopper facet brought on by knowledge switch dimension. The above instance, nonetheless, is totally different in that it is a problem on cloud facet – the info on cloud is inconsistent).
I additionally discover the next within the doc. Nonetheless, I do not assume it helps within the above instance, as a result of IIUC CloudKit can solely detect battle when the adjustments on the identical report however the adjustments in step 1 and step 2 are on totally different data.
As a result of data can change between the time you fetch them and the time you save them, the save coverage determines whether or not new adjustments overwrite current adjustments. By default, the operation reviews an error when there’s a more recent model on the server.
If the above understanding is appropriate, nonetheless, I do not perceive why the identical doc has the next requirement, which means CloudKit does not enable invalid reference:
When creating two new data which have a reference between them, use the identical operation to avoid wasting each data on the similar time.
Q2) Suppose CloudKit permits invalid reference on cloud facet (that’s, machine B efficiently saves the change in step 2 to cloud) , I’m wondering what’s one of the best apply to take care of it?
I believe the difficulty is totally different from the non-compulsory relation requirement in Core Information when utilizing CloudKit, as a result of in that case the info is constant on cloud facet and finally the shopper will obtain full knowledge. Within the above instance, nonetheless, the info on cloud is inconsistent so the shopper has to treatment it in some way (though shopper has little data serving to it).
One method I consider is to keep away from the difficulty within the first place. My concept is to take care of a counter within the database and requires shopper to extend the counter (it isn’t Lamport clock. BTW, is it attainable to make use of Lamport clock on this case?) when making any change. This could assist CloudKit to detect battle (although I am unable to assume out a very good technique on how shopper ought to take care of it. A easy one is maybe to immediate consumer to pick out one copy). Nonetheless, this method successfully makes use of cloud as a centralized server, which I think is not the standard approach how folks use CloudKit, and it requires shoppers to take care of native counter worth in varied conditions. I’m wondering what is the typical method? Am I lacking one thing?
Thanks for any assist.