CloudKit Sharing

后端 未结 2 1357
孤独总比滥情好
孤独总比滥情好 2021-01-01 02:18

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 req

2条回答
  •  星月不相逢
    2021-01-01 03:17

    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")
                }
            }
        }
    }
    

提交回复
热议问题