Async Data with iCloud

DESCRIPTION I have an App use iCloud to save data.

  1. The App had a CoreData ManagedObject 'Product', 'Product' Object had an attribute name 'count' and it is a Double Type.
  2. I need to synchronises 'count' property across multiple devices.

for example:

  1. I have a devices A、B.
  2. A device set 'Product.count' = 100.
  3. B device set 'Product.count' = 50.
  4. I hope the 'Product.count' == 150 that results.
  5. how to synchronises the 'Product.count' == 150 for multiple devices.
  6. If I have more devices in future, How to get the latest 'Product.count' that it is correct result.
Answered by DTS Engineer in 826728022

Given that you are already using Core Data, you can consider using NSPersistentCloudKitContainer.

If the data you would synchronize is as simple as several values, you can consider using the CloudKit framework as well, which gives you more flexibility.

You might want to start with looking into the above APIs, and follow up with your further questions, if any.

Best,
——
Ziqiao Chen
 Worldwide Developer Relations.

Given that you are already using Core Data, you can consider using NSPersistentCloudKitContainer.

If the data you would synchronize is as simple as several values, you can consider using the CloudKit framework as well, which gives you more flexibility.

You might want to start with looking into the above APIs, and follow up with your further questions, if any.

Best,
——
Ziqiao Chen
 Worldwide Developer Relations.

I've synced the device's data to iCloud but my issue is more like asynchronous cumulative number problem. We knew the result would be wrong.
We know that to get the right answer that need to change from asynchronous to synchronous. I think of putting scripts on the CloudKit. But the server does not seem to support such an operation.

You are right that CloudKit doesn't support running a trigger-like script on the server side.

You can probably consider using your own data structure to achieve the goal. For example, you can create a CloudKit record type to store the values from all clients, and have the clients sum up the values when needed. The design and implementation are all up to you though.

Best,
——
Ziqiao Chen
 Worldwide Developer Relations.

it's a good idea. I'll try it.

Async Data with iCloud
 
 
Q