热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

在CloudKit中保存对象数组-SavingAnArrayOfObjectsInCloudKit

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 个解决方案

#1


3  

For the Item records you need a CKReference to the Collection. You will then be able to set a CKReferenceAction on that. You don't need to create a CKReference list on the Collection.

对于Item记录,您需要CKReference到Collection。然后,您就可以在其上设置CKReferenceAction。您无需在Collection上创建CKReference列表。

Having a list of CKReference objects is only an option when you are planning to use CKReferenceAction.None Which would mean that there is not a strict relation between the two recordTypes

当您计划使用CKReferenceAction.None时,只有一个CKReference对象列表是一个选项。这意味着两个recordTypes之间没有严格的关系

If you have a Collection object, then you can easily query the Item recordType using a predicate that Checks if the CKReference is that of the Collection.

如果您有一个Collection对象,那么您可以使用一个谓词轻松查询Item recordType,该谓词检查CKReference是否为Collection的值。

Usually there is no need to save multiple records. Once you have created a Collection with Items the relation can remain unchanged. If you do need to change multiple records, then you could try using the CKModifyRecordsOperation which has support for saving multiple items in one action.

通常不需要保存多条记录。使用Items创建Collection后,关系可以保持不变。如果确实需要更改多个记录,则可以尝试使用CKModifyRecordsOperation,它支持在一个操作中保存多个项目。

When linking existing Item recordTypes to a Collection, you do need to save each Item because it has a CKReference to the Collecion. The Item has changed, so it must be saved.

将现有的Item recordTypes链接到Collection时,您需要保存每个Item,因为它有一个CKReference给Collecion。项目已更改,因此必须保存。


推荐阅读
author-avatar
平凡小迪
这个家伙很懒,什么也没留下!
Tags | 热门标签
RankList | 热门文章
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有