作者:平凡小迪 | 来源:互联网 | 2023-05-17 05:44
ImconfusedabouthowtobestsaveanobjectthatcontainsanarrayofotherobjectsinCloudKit.我
I'm confused about how to best save an object that contains an array of other objects in CloudKit.
我对如何最好地保存包含CloudKit中其他对象数组的对象感到困惑。
Say I have a todo list app, which has different collections of items. How would I go about saving/fetching a whole collection of items?
假设我有一个待办事项列表应用程序,它有不同的项目集合。我如何保存/获取整个项目集合?
Would I have a Record type of Collection, which would have a String Attribute called "Name", and then a Reference List Attribute called "Items"?
我是否有一个Record类型的Collection,它有一个名为“Name”的String属性,然后是一个名为“Items”的引用列表属性?
I know that the Record type of Item needs to have a Reference Attribute called "Collection", because of how CloudKit references from a child object to its parent.
我知道Record类型的Item需要有一个名为“Collection”的引用属性,因为CloudKit如何从子对象引用它的父对象。
I have managed to save a Collection without any instances of Item with the following code
我已设法使用以下代码保存没有任何Item实例的Collection
func addCollection(collection: Collection!, completion: (error: NSError!) -> ()) {
if collection == nil
return
}
let collectiOnRecord= CKRecord(recordType: "Collection")
CollectionRecord.setObject(collection.name, forKey: "Name")
privateDB.saveRecord(collectionRecord) {
record, error in
dispatch_async(dispatch_get_main_queue()) {
completion(error: error)
}
}
}
The other option is when saving a Collection, to loop through all instances of Item and also save those individually, their Reference Attribute to Collection making the connection on the CloudKit side, but this seems like way too many network calls.
另一个选择是在保存Collection时,循环遍历Item的所有实例并单独保存它们,它们的Reference Attribute to Collection在CloudKit端进行连接,但这似乎是太多的网络调用。
1 个解决方案