简体   繁体   English

如何在CloudKit中创建记录作为孩子的记录列表?

[英]How do I make a record in CloudKit have a list of records as a child?

I want to have a record in the private database that represents a shopping list, and then I want to have child records of that shopping list that represent items on the shopping list. 我想在私人数据库中有一个代表购物清单的记录,然后我希望该购物清单的子记录代表购物清单上的项目。 I'm wondering if that's possible, and if it is, should I implement it as just child records of the shopping list record or whether I should make "items" an attribute of the "shopping lists" record with NSArray type and then add the item CKRecords to that array. 我想知道这是否可能,如果是的话,我应该将其实现为购物清单记录的子记录,还是我应该使用NSArray类型将“items”作为“购物清单”记录的属性,然后添加项目CKRecords到该数组。

It's not clear in the documentation whether a CKRecord can be a child of another CKRecord. 在文档中不清楚CKRecord是否可以是另一个CKRecord的孩子。

Have one record type to represent your shopping list and another record type to represent items on the shopping list. 有一种记录类型代表您的购物清单,另一种记录类型代表购物清单上的项目。

In your shopping list record type, have a field that is an array of CKReferences, each of which points to an item record. 在您的购物清单记录类型中,有一个CKReferences数组的字段,每个字段指向一个项目记录。

In your items record type have a field that is a CKReference that points back to the shopping list (eg called isOnShoppingList) and set its CKReferenceAction to DeleteSelf. 在您的项目中,记录类型具有一个CKReference字段,该字段指向购物清单(例如,称为isOnShoppingList)并将其CKReferenceAction设置为DeleteSelf。 This will ensure that the item gets deleted if the shopping list gets deleted. 这将确保在删除购物清单时删除该项目。

(Edit: added reverse reference with CKReferenceAction = DeleteSelf.) (编辑:使用CKReferenceAction = DeleteSelf添加反向引用。)

According to Apple , it's best to "organize your records around a central record type". 根据Apple的说法 ,最好“围绕中央记录类型整理记录”。 This could be your shopping list record. 这可能是您的购物清单记录。 Your shopping list item record would then be one of a possible many "additional record types to support the primary type". 您的购物清单项目记录将是可能的许多“支持主要类型的其他记录类型”之一。

To associate the two, Apple suggests we use something they call a "back reference" in the "Introducing CloudKit (Session 208) at WWDC 2014 . This would consist of having your shopping list item record maintain a reference back to the shopping list to which it is associated, rather than the other way around. This makes queries less intensive, because you can simply fetch list item records using the list's Id, rather than searching an array of references with each item for each list. Also, this design more closely aligns to the normal app interface design of displaying a list of shopping lists, then showing a list of items once a shopping list is selected. 为了将这两者联系起来,Apple建议我们在WWDC 2014“介绍CloudKit(会话208) ”中使用他们称之为“后向参考”的内容。这包括让您的购物清单项目记录保留一个参考回到购物清单它是相关联的,而不是相反的。这使查询不那么密集,因为您可以使用列表的Id简单地获取列表项记录,而不是搜索每个列表的每个项目的引用数组。此外,这个设计更紧密与显示购物清单列表的常规应用程序界面设计对齐,然后在选择购物清单后显示项目列表。

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

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