简体   繁体   中英

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 (eg 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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM