Search code examples
iosswiftcloudkit

Error using CKSubscription by Zone


I have been experimenting with CloudKit, and so far it's working fine. However, I've been having a hard time getting zone based subscription to work. Has anybody worked on it?. Here the code I tried :

// MARK: - Subscriptions

func registerSubscriptionByZone(){

    let container = CKContainer(identifier: "iCloud.com.MyDataInCloudKit")
    let database = container.publicCloudDatabase

    let recordZone = CKRecordZone.defaultRecordZone()

    let zoneSubscription = CKSubscription(zoneID: recordZone.zoneID, options:CKSubscriptionOptions.allZeros)
    zoneSubscription.notificationInfo = CKNotificationInfo()
    zoneSubscription.notificationInfo.alertBody = "Change in Zone"
    self.database.saveSubscription(zoneSubscription) {
        subscription, error in
        if error != nil {
            println(error.localizedDescription)
        } else {
            println("subscription saved!")
        }
    }

}

I get the error: Error saving record subscription with id EB6C9478-0A38-4C01-A473-6C09A57B2E8B to server: no valid zone specified


Solution

  • From the CloudKit design guide at: https://developer.apple.com/library/ios/documentation/General/Conceptual/iCloudDesignGuide/DesigningforCloudKit/DesigningforCloudKit.html

    Zones are a useful way to arrange a discrete group of records but are supported only in private databases. Zones cannot created in a public database.