Joris416
Joris416

Reputation: 4882

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

I have this piece of code:

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

It's supposed to save a profile property on a Check object in my Checks RecordZone. When I trigger this function, I get this error:

 <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 ...

}>

I have attempted several things:

Please be aware that sharedstore.checksID is an actual ID instead of a string, which is created on app launch when all record zones are fetched.

How would I be able to solve this error? Any suggestion would be highly appreciated.

Upvotes: 5

Views: 698

Answers (1)

Edwin Vermeer
Edwin Vermeer

Reputation: 13127

you have to set the zoneID on the operation wit something like:

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

Update: As discussed below the .zoneID is only available on a CKQqueryOperation and not a CKModifyRecordsOperation

I believe it's only possible to set a CKReference to a record that is in the same zone. Is that the case? If not, then could you try saving the record without that reference?

From the documentation:

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

update: it is possible to add a reference across databases (public and private) provided the source and target of the reference are in the default zone.

Upvotes: 6

Related Questions