CloudKit共享 [英] CloudKit Sharing

查看:73
本文介绍了CloudKit共享的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我无法理解某些CloudKit共享概念,并且WWDC 2016 CloudKit的新功能视频似乎无法解释允许用户共享和访问共享记录所需的一切。

I am having trouble understanding some of the CloudKit sharing concepts and the WWDC 2016 "What's new in CloudKit" video doesn't appear to explain everything that is required to allow users to share and access shared records.

我已经成功创建了一个应用程序,该应用程序允许用户在其私有数据库中创建和编辑记录。
我还能够创建一个共享记录,并使用提供的共享UIController共享该记录。参与者用户可以成功接收并接受此记录,但我不知道如何查询和显示此共享记录。

I have successfully created an app that allows the user to create and edit a record in their private database. I have also been able to create a Share record and share this using the provided sharing UIController. This can be successfully received and accepted by the participant user but I can't figure out how to query and display this shared record.

该应用程序在用户私有数据库中创建一个 MainZone,然后在此 MainZone中创建一个CKRecord。然后,我创建并保存一个CKShare记录,并使用它显示UICloudSharingController。

The app creates a "MainZone" in the users private database and then creates a CKRecord in this "MainZone". I then create and save a CKShare record and use this to display the UICloudSharingController.

如何查询sharedDatabase以便访问此记录?我尝试使用与privateDatabase中使用的查询相同的查询,但出现以下错误:

How do I query the sharedDatabase in order to access this record ? I have tried using the same query as is used in the privateDatabase but get the following error:

"ShareDB can't be used to access local zone"

编辑

我发现了问题-我需要在AppDelegate中处理接受的记录。现在它们出现在CloudKit仪表板中,但我仍然无法查询它们。看来我可能需要获取sharedDatabase MainZone才能对其进行查询。

I found the problem - I needed to process the accepted records in the AppDelegate. Now they appear in the CloudKit dashboard but I am still unable to query them. It seems I may need to fetch the sharedDatabase "MainZone" in order to query them.

推荐答案

Dude,我明白了:您需要获取该共享记录的 CKRecordZone 。您可以执行以下操作:

Dude, I got it: First you need to get the CKRecordZone of that Shared Record. You do it by doing the following:

let sharedData = CKContainer.default().sharedCloudDatabase
    sharedData.fetchAllRecordZones { (recordZone, error) in
        if error != nil {
            print(error?.localizedDescription)
        }
        if let recordZones = recordZone {
            // Here you'll have an array of CKRecordZone that is in your SharedDB!
        }
    }

现在,有了该数组,您将拥有要做的是正常获取:

Now, with that array in hand, all you have to do is fetch normally:

func showData(id: CKRecordZoneID) {

    ctUsers = [CKRecord]()

    let sharedData = CKContainer.default().sharedCloudDatabase
    let predicate = NSPredicate(format: "TRUEPREDICATE")
    let query = CKQuery(recordType: "Elder", predicate: predicate)

    sharedData.perform(query, inZoneWith: id) { results, error in
        if let error = error {
            DispatchQueue.main.async {
                print("Cloud Query Error - Fetch Establishments: \(error)")
            }
            return
        }
        if let users = results {
            print(results)
            self.ctUsers = users
            print("\nHow many shares in cloud: \(self.ctUsers.count)\n")
            if self.ctUsers.count != 0 {
                // Here you'll your Shared CKRecords!
            }
            else {
                print("No shares in SharedDB\n")
            }
        }
    }
}

这篇关于CloudKit共享的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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