简体   繁体   English

使用CloudKit保持NSArray的顺序

[英]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. 我目前正在设计一个基于CloudKit的同步解决方案,我想知道最好的方法是保持列表(在我的情况下是一个数组)的云项( CKRecord对象)的顺序是一致的。

Apple advises against holding a reference to child objects and instead just reference the parent object with a CKReference from the child. Apple建议不要持有对子对象的引用,而只是使用来自子对象的CKReference引用父对象。 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). 1)同步具有标识符数组的额外记录(aka清单),每个标识符标识CKRecord对象(以及对应的本地模型对象)。

2) Create a parent object which holds an array of references ( CKReference objects) to it's child objects. 2)创建一个父对象,它包含一个引用数组( CKReference对象)到它的子对象。 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. Apple确实建议不要保留对子对象的引用,但这并不意味着你不能只在你的对象中包含一个带有记录Id的数组。 These does not have to be CKReference objects. 这些不必是CKReference对象。 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. 更改订单时,您只需更改3条记录。 1 the record that did point to your record, the new record that will point to your record and the record itself. 1指向记录的记录,指向记录的新记录和记录本身。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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