CloudKit-如何共享多个记录并检索共享记录? [英] CloudKit - How to share multiple records and retrieve shared records?

查看:81
本文介绍了CloudKit-如何共享多个记录并检索共享记录?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我观看了最后的 WWDC 2016 CloudKit的新增功能,以了解如何使用 CKShare

I watched last WWDC 2016 What's New with CloudKit to understand how to share records with other users using CKShare

单个记录共享与其他用户共享记录:

我能够共享和检索单个记录

I am able to share and retrieve a single record

即,如果 xyz@gmail.com 已创建并共享单个记录记录到 abc@gmail.com

i.e if xyz@gmail.com has created and shared a single record to abc@gmail.com

多条记录共享:

假设有10条记录, xyz@gmail.com 希望共享给 abc@gmail.com 。当用户 xyz@gmail.com 与用户 abc@gmail.com

let's say there are 10 records and xyz@gmail.com wants to share to abc@gmail.com. I am facing the issue when user xyz@gmail.com shares multiple records to user abc@gmail.com

到目前为止,我已经尝试过:

首先,我创建了3条记录记录:

First I created 3 note records:

Note1

Note2(将父级设置为Note1)

Note2 ( set parent as Note1 )

Note3(将父级设置为Note1)

Note3 ( set parent as Note1 )

我使用以下代码共享了Note1(父记录):

I shared Note1 ( Parent record ) with below code:

代码-共享记录

let controller = UICloudSharingController { controller,
        preparationCompletionHandler in

        let share = CKShare(rootRecord: self.parentRecord!)
        share[CKShareTitleKey] = "note" as CKRecordValue
        share.publicPermission = .readOnly

        let operation = CKModifyRecordsOperation(
              recordsToSave: [self.parentRecord!, share],
              recordIDsToDelete: nil)

        operation.perRecordCompletionBlock = { (record, error) in
            if let error = error {
                print("CloudKit error: \(error.localizedDescription)")
            }
        }

        operation.modifyRecordsCompletionBlock = { records, recordIDs, error in

            if error != nil {
                print(error?.localizedDescription ?? "Error")
            } else{
                print("Success")
                preparationCompletionHandler(share,CKContainer.default(), error)
            }
        }

        CKContainer.default().privateCloudDatabase.add(operation)

    }

    controller.availablePermissions = [.allowPrivate, .allowReadOnly]
    controller.delegate = self
    present(controller, animated: true)

并检索到共享笔记使用以下代码:

and retrieved shared-note with below code:

代码-从共享笔记中读取数据

func application(_ application: UIApplication, userDidAcceptCloudKitShareWith cloudKitShareMetadata: CKShareMetadata) {
    let acceptSharesOperation = CKAcceptSharesOperation(
        shareMetadatas: [cloudKitShareMetadata])

    acceptSharesOperation.perShareCompletionBlock = {
        metadata, share, error in
        if error != nil {
            print(error?.localizedDescription)
        } else {

            let operation = CKFetchRecordsOperation(
                recordIDs: [cloudKitShareMetadata.rootRecordID])

            operation.perRecordCompletionBlock = { record, _, error in

                if error != nil {
                    print(error?.localizedDescription)
                }

                if let shareRecord = record {
                    DispatchQueue.main.async() {
                        // Shared record successfully fetched. Update user
                        // interface here to present to user.
                        print("\(shareRecord["id"]!)") // id of note
                        print("\(shareRecord["text"])") // text of note
                        print("Shared record successfully fetched")
                    }
                }
            }

            operation.fetchRecordsCompletionBlock = { (recordsWithRecordIDs,error) in
                if error != nil {
                    print(error?.localizedDescription)
                }else {
                    if let recordsWithRecordIDs = recordsWithRecordIDs {
                        print("Count \(recordsWithRecordIDs.count)")
                    }
                }
            }
            CKContainer.default().sharedCloudDatabase.add(operation)
        }
    }

    CKContainer(identifier: cloudKitShareMetadata.containerIdentifier)
        .add(acceptSharesOperation)
}

上述方法仅提供父注释数据(仅根/父级)

Above method gives only parent note data ( root/parent only )

查询:

1)如何获取其他孩子的笔记记录? (我用学期孩子来理解)

1) How to fetch other children note records? ( I used term children for understanding purpose )

2)我是否需要每次检查是否有新的共享记录

2) Do I need to check every time whether there are new shared records or not

因为我还没有找到任何好的教程和Apple官方文档的来源。您是否建议我共享和检索多个记录的方法?

As I haven't found any good tutorials and the source from official Apple docs. Would you suggest me the approach to share and retrieve multiple records?

预先感谢..!

推荐答案

我相信Thunk的回答是不正确的,因为您不必使用给定记录的父引用来获取任何记录。首先,您将在哪里搜索? CKShare根记录的子记录不会出现在共享数据库中,并且您无权访问其他用户的私有区域。幸运的是,CloudKit将向您推送CKShare根记录引用的所有子记录。以下方法也可以解决第二个问题,以后会自动将所有更改通知您!

I believe Thunk's answer is incorrect, since you should not have to fetch any records with the parent reference to a given record. First, where would you search? The child records of the CKShare root record do not appear in your shared database and you don't have access to the other user's private zone. Luckily, CloudKit will push to you all child records referenced by the CKShare's root record. The following approach will also solve the second problem, you will be notified of any and all future changes automatically!

关键是简单地添加 CKDatabaseSubscription 到CloudKit容器中的共享数据库。假设您是客户。每当其他用户与您共享一条记录时,共享所引用的CKShare记录和基础根记录都位于该用户的私有数据库中的自定义区域中。当您接受共享(触发应用程序委托方法 userDidAcceptCloudKitShareWith )时,相同 CKShare和基础记录现在对您可见,除了它们在您的共享数据库中都可见。如果您已订阅共享数据库,则订阅将在您接受共享后立即生效并通知您。现在,您可以使用 CKFetchDatabaseChangesOperation 获取共享数据库中的所有更改。在 recordZoneWithIDChangedBlock 中,将提供共享用户的自定义区域的 CKRecordZoneID 。您可以使用它触发 CKFetchRecordZoneChangesOperation (确保在共享数据库中执行此操作!),该操作将返回CKShare,基础根记录和所有记录具有对基础引用记录的父引用

The key is to simply add a CKDatabaseSubscription to the shared database in the CloudKit container. Let's assume you are the client. Anytime another user share's a record with you, the CKShare record and the underlying root record referenced by the share are both situated in a custom zone in that user's private database. When you accept the share (triggering an application delegate method userDidAcceptCloudKitShareWith), the same CKShare and underlying record are now visible to you, except that they are both visible in your shared database. If you have subscribed to the shared database, the subscription will kick in and notify you right after you accept the share. Now you can fetch all changes in the shared database using CKFetchDatabaseChangesOperation. In the recordZoneWithIDChangedBlock, the CKRecordZoneID of the sharing user's custom zone will be given. You use this to trigger a CKFetchRecordZoneChangesOperation (be sure to perform this operation in the shared database!), which returns the CKShare, the underlying root record, and all records that have a parent reference to the underlying referenced record.

因此,通过简单的数据库订阅,您可以自动触发并下载所有共享,根记录和子项!

So with a simple database subscription you can automatically trigger and download all shares, root records, and children!

这篇关于CloudKit-如何共享多个记录并检索共享记录?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆