cloudkitsubscriptionzone

Create subscription for custom zone in private database fails


I create a custom zone in private database successfully and then try to create a subscription for that custom zone, but subscription creation fails with error: "An error indicating that some items failed, but the operation succeeded overall.".

ios document suggests using CKRecordZoneSubscription to create zone subscription. But I still get errors. How can I fix this?

var ContactZonePrivateDb: CKRecordZone  = CKRecordZone(zoneName: Cloud.PrivateZone.Contact.ZoneName)


func createCustomContactInfoZone (completion: @escaping (Error?) -> Void){

    let status = UserDefaults.standard.bool(forKey: Cloud.PrivateDB.CustomZoneCreated)

    if status == true {
        return
    }

    let createZoneGroup = DispatchGroup()
    createZoneGroup.enter()

    let createZoneOperation = CKModifyRecordZonesOperation(recordZonesToSave: [ContactZonePrivateDb], recordZoneIDsToDelete: [] )

        createZoneOperation.modifyRecordZonesCompletionBlock = { (saved, deleted, error) in
            if error != nil  {
                if let ckerror = error as? CKError {
                    self.aErrorHandler.handleCkError(ckerror: ckerror)
                }
                completion(error)
            }
            UserDefaults.standard.set(true, forKey: Cloud.PrivateDB.CustomZoneCreated)
            self.subscribePrivateZoneContact()
            completion(nil)
        }
    createZoneGroup.leave()

    createZoneOperation.qualityOfService = .userInitiated
    self.privateDB?.add(createZoneOperation)
} 

func subscribePrivateZoneContact() {
    let status = UserDefaults.standard.bool(forKey: Cloud.PrivateZone.Contact.SubscriptionID)
    if (status == true) {
        return
    }
    let subscriptionZone = CKRecordZoneSubscription(zoneID: ContactZonePrivateDb.zoneID)
    let operation = CKModifySubscriptionsOperation(subscriptionsToSave: [subscriptionZone], subscriptionIDsToDelete: nil)

    operation.modifySubscriptionsCompletionBlock = { saved, deleted, error in
        guard error == nil else {
            if let ckerror = error as? CKError {
                self.aErrorHandler.handleCkError(ckerror: ckerror)

            }
            return
        }
        UserDefaults.standard.set(true, forKey:  Cloud.PrivateZone.Contact.SubscriptionID)
        DispatchQueue.main.async {
            print("Successfully added Private zone subscription.\(self.ContactZonePrivateDb.zoneID)")
        }

    }
    operation.qualityOfService = .userInitiated
    self.privateDB?.add(operation)
}

Solution

  • Found that in order to create a subscription on custom zone, you have to create a record in the custom zone first. But it is not the case for other subscriptions. wonder why?