Since Core Data doesn't support ordered one to many relationships when using CloudKit, I am thinking of adding a order: Int
field to the entity like here to be able to implement ForEach {}.onMove
.
Let's say a user currently has 3 items with orders 1, 2 and 3. On the first offline device, they swap item 3 with item 1 to get: "Item 3" (1), "Item 2" (2), "Item 1" (3). Then, on the second device, they move item 3 before item 2 to get: "Item 1" (1), "Item 3" (2), "Item 2" (3).
Assuming that the "last write wins", after syncing, "Item 1" will have an order of 3 because of the first device and the second device didn't change it. "Item 2" will also have an order of 3 because the second device wins. The final result will be: "Item 3" (2), "Item 2" (3), "Item 1" (3).
Another scenario is that both offline devices create a new item with order 4. If I sort by \.order
and different items have the same value for order
, then the order of the result won't be guaranteed.
Is there a recommended way to handle these conflicts? The default "Reminders" app allows for re-ordering lists and their items, so it should be possible, assuming that Core Data with CloudKit is being used. (An alternative to Core Data is Realm, and they have a List
instead of an ordered set. When using Flexible Sync, the conflicts are handled on the server.)