Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Keep order of NSArray with CloudKit

I'm currently designing a CloudKit based syncing-solution and I wondered what the best way is to keep the order of a list (an array in my case) of cloud items (CKRecord objects) consistent.

Apple advises against holding a reference to child objects and instead just reference the parent object with a CKReference from the child. This works fine if you want to query your items based on one of it's properties (e.g. a creation date), but not if you have have an order which is determined by the user.

I've come up with two different approaches which are based on the same idea: maintain a manifest of identifiers to manage the item's position.

1) Sync an extra record (aka manifest) which has an array of identifiers, each identifying a CKRecord object (and the corresponding local model object).

2) Create a parent object which holds an array of references (CKReference objects) to it's child objects. This array maintains the given order.

I feel that this is not quite the best possible solution to this problem and I would be glad to hear what you think.

like image 617
JanApotheker Avatar asked Feb 27 '15 04:02

JanApotheker


1 Answers

Apple does advises against holding a reference to child objects but that does not mean you can't just include an array with record Id's in your object. These does not have to be CKReference objects. You could just save an array of string values.

I think you are right that this would be the best/easiest approach for maintaining a sort order.

One other solution would be to create a linked list. Each record then needs a reference to the next record. When changing the order, then you only need to change 3 records. 1 the record that did point to your record, the new record that will point to your record and the record itself.

like image 135
Edwin Vermeer Avatar answered Nov 07 '22 19:11

Edwin Vermeer