1

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)
}
vrao
  • 545
  • 2
  • 12
  • 33

2 Answers2

1

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?

vrao
  • 545
  • 2
  • 12
  • 33
0

If you delete your subscriptions in the CloudKit Dashboard and then run your subscription creation code once, does it still show an error?

The reason I ask is because I believe you will see that error if you attempt to recreate a subscription by the same name. This is okay to do and the error can be ignored.

Clifton Labrum
  • 13,053
  • 9
  • 65
  • 128
  • I deleted all the subscriptions and started fresh, still subscription on custom zone fails – vrao Nov 30 '19 at 22:12
  • I create the subscription for the custom zone and before adding the subscription to CKModifySubscriptionCompletieoBlock, in debugger I print the subscription to be added it gives; (CKRecordZoneSubscription) $R0 = 0x00000002821828c0 { CloudKit.CKSubscription = { baseNSObject@0 = { isa = CKRecordZoneSubscription } _subscriptionID = 0x0000000282183000 "EA78DC8F-D377-4CFD-9F39-0E0BDA477C59" _subscriptionType = 2 _notificationInfo = nil _zoneID = 0x0000000283487e40 _recordType = nil _predicate = nil _subscriptionOptions = 0 } } – vrao Nov 30 '19 at 22:45
  • posting the debugger output continuation: CKRecordZoneSubscription) $R2 = 0x00000002821828c0 { CloudKit.CKSubscription = { baseNSObject@0 = { isa = 0x00000002821828c0 } _subscriptionID = 0x0000000282183000 "EA78DC8F-D377-4CFD-9F39-0E0BDA477C59" _subscriptionType = 2 _notificationInfo = nil _zoneID = 0x0000000283487e40 _recordType = nil _predicate = nil _subscriptionOptions = 0 } } - Does the debugger output show that there are two custom zone subscriptions already? I cant see in dashboard and also it failed everytime i created – vrao Nov 30 '19 at 22:45
  • so what are the two subscriptions show in debugger for custom zone, I am puzzled? – vrao Nov 30 '19 at 22:48