Unable to fetch records in a sharedCloudDatabase custom Zone using CloudKit

℡╲_俬逩灬. 提交于 2021-02-08 10:35:53

问题


I am trying to fetch CloudKit records from a custom Zone in a sharedDatabase.

The zone has been created correctly during the share process. So I assume that the zone is correctly a shared custom zone (it is indeed in my CloudKit user dashboard, appearing under the sharedDatabase of the default container).

Even with this simple piece of code to retrieve records:

func loadRecords() {
    let database = CKContainer.default().sharedCloudDatabase
    let query = CKQuery(recordType: "Items", predicate: NSPredicate(format: "TRUEPREDICATE", argumentArray: nil))
    let operation = CKQueryOperation(query: query)
    let zone = CKRecordZone(zoneName: "MyCustomZone")
    var fetchedRecords: [CKRecord] = []

    database.perform(query, inZoneWith: zone.zoneID) { (records, error) in
        if let error = error {
            print(error)
        }
        else if let records = records, let firstRecord = records.first {
            print(firstRecord)
        }
    }
}

I keep receiving this error: (sorry was missing in previous post!)

<CKError 0x280950840: "Invalid Arguments" (12/1009); "Only shared zones can be accessed in the shared DB">

Any idea about what I am missing? Thank you!


回答1:


Right, I finally figured it out. Thanks to this additional post here on StackOverflow: CloudKit Sharing

You indeed have to fetch your custom zone. But it is not enough to give it a name such as "MyCustomZone" because in your shared DB, it becomes MyCustomZone: _acBxxxyyyzzz. A suffix added by CloudKit.

If you don't fetch all your shared zones, you never have the right custom zone from which you need to pickup the record.



来源:https://stackoverflow.com/questions/56163096/unable-to-fetch-records-in-a-sharedclouddatabase-custom-zone-using-cloudkit

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!