0

I have an app for which I want to add the possibility to backup data to iCloud using CloudKit. The "backup" part seems to work correctly (my records are in the private database, since they are ... well, private).

Now I would like to use CKSubscriptions to keep all my devices in sync with the same data. I tried to implement a CKSubscription to monitor record creation / update / deletion based on a query (not based on zones).

func subscribe() {

  let options = CKSubscriptionOptions.FiresOnRecordCreation |
  CKSubscriptionOptions.FiresOnRecordDeletion |
  CKSubscriptionOptions.FiresOnRecordUpdate


  let predicate = NSPredicate(value: true) // get all the records for a given type
  let subscription = CKSubscription(recordType: "Stocks",
  predicate: predicate, subscriptionID: subscriptionID,
  options: options)

  subscription.notificationInfo = CKNotificationInfo()
  subscription.notificationInfo.alertBody = ""

  db.saveSubscription(subscription, completionHandler: {
  subscription, error in
    if (error != nil) {
      println("error subscribing: \(error)")
    } else {
      println("subscribed!")
    }
  })
}

Until now, I haven't been able to trigger a notification to my device.

I know that you can create a subscription based on zones. Zones are in the private DB, so I suppose that CKSubscriptions could work in the private DB. But I didn't want to implement zones (that I don't need otherwise).

Question: is there an issue with CKSubscriptions in the private DB based on a query ?

Frederic Adda
  • 5,905
  • 4
  • 56
  • 71
  • Update to my question : if in my options, I only have CKSubscriptionOptions.FiresOnRecordCreation, it works. But if I put another option, or a combination of options, then it doesn't work anymore. Bug reported and DTS support asked, for the moment without any result. – Frederic Adda Jun 22 '15 at 15:21

1 Answers1

1

This should work. Non zone subscriptions (query based subscriptions) are supported on private databases. Did you add the code to receive notifications in your AppDelegate?

harryhorn
  • 892
  • 6
  • 8
  • Yes, I did. I added functions didRegisterForRemoteNotificationsWithDeviceToken, didFailToRegisterForRemoteNotificationsWithError and didReceiveRemoteNotification. I also activated "Remote notifications" in the Background Modes capabilities of my target. – Frederic Adda May 20 '15 at 18:40