Cloudkit: "ZoneId provided doesn't match target zone"

Cloudkit: "ZoneId provided doesn't match target zone"

我有这段代码:

func saveProfileForCheck(check: Check) {
    let toSave = CKRecord(recordType: "Check", zoneID: sharedStore.checksID!)
    let profile = check.profile as Profile!
    toSave.setValue(NSString(format: "%i", check.closed), forKey: "closed")
    toSave.setValue(check.date, forKey: "date")
    toSave.setValue(NSString(format: "%i", check.paid), forKey: "paid")
    toSave.setValue(CKReference(recordID: profile.dbRecordID, action: CKReferenceAction.DeleteSelf), forKey: "profile")

    let operation = CKModifyRecordsOperation(recordsToSave: [toSave], recordIDsToDelete: [check.id])
    operation.modifyRecordsCompletionBlock = { (savedRecords, deletedRecordIDs, error) in
        if error != nil {
            self.delegate?.cloudKitReturnedError(error!)
        } else {
            self.modifiedCheck(check, newRecord: savedRecords![0] as CKRecord )

        }
    }
    privateDB.addOperation(operation)
}

它应该在我的 Checks RecordZone 中的 Check 对象上保存 profile 属性。当我触发这个函数时,我得到这个错误:

 <CKError 0x7f8c2046d630: "Partial Failure" (2/1011); "Failed to modify some records"; uuid = EF4CCE3F-3CDB-4506-BA43-464D7D9BD0F6; container ID = "mycontainer"; partial errors: {
130BD962-295C-4601-9343-2E4F4014C8C7:(Checks:__defaultOwner__) = <CKError 0x7f8c2045c680: "Invalid Arguments" (12/2006); server message = "ZoneId provided doesn't match target zone">
... 1 "Batch Request Failed" CKError's omited ...

}>

我尝试了几件事:

请注意,sharedstore.checksID 是一个实际 ID 而不是字符串,它是在应用程序启动时获取所有记录区域时创建的。

我该如何解决这个错误?任何建议将不胜感激。

你必须在操作上设置 zoneID,例如:

    operation.zoneID = CKRecordZoneID(zoneName: sharedStore.checksID!, ownerName: "your name")

更新:如下所述,.zoneID 仅适用于 CKQqueryOperation 而不是 CKModifyRecordsOperation

我认为只能将 CKReference 设置为同一区域中的记录。是这样吗?如果没有,那么您可以尝试在没有该参考的情况下保存记录吗?

来自文档:

Treat each custom zone as a single unit of data that is separate from every other zone in the database. Inside the zone, you add records as you would anywhere else. You can also create links between the records inside a zone by using the CKReference class. However, the CKReference class does not support cross-zone linking, so each reference object must point to a record in the same zone as the current record

更新: 如果引用的源和目标位于默认区域中,则可以跨数据库(public 和私有)添加引用。