Renee Olson
Renee Olson

Reputation: 267

Struggling to Understand CKSubscriptions in CloudKit

I've been reading about and trying to use the CKSubscription feature for weeks, but I can't find info about some general questions. I've read Apple docs, online tutorials, books I bought, and questions here on SO, but I still don't understand the fundamentals I think. Any help is very much appreciated.

Here are questions I cannot find answers to:

1). What is the purpose of the subscriptionID? The convenience init does not include it, so why is it needed in the designated init? If you use it, is it the same for all users of the app?

2). I saw someone mention here that you can unregister a subscriptionID. Why or how would you do this?

3). Can subscriptions be setup in both public or the user's private database?

4). If I have a query based subscription that is the same for all users, will there only ever be 1 subscription listed in the database?

For instance, I'm having trouble getting notifications to work with my specific use case. It's not a problem in my setup, as I can get a True predicate to work and the notification comes. So I must not understand the fundamentals of how subscriptions work still.

I'm trying to setup a subscription that fires whenever a new record is created when a different user makes a comment on a post. This new record will then contain a reference to the user who created the post. The only subscription I see in the database for both users is - Notifications.user (equals reference). So, I'm assuming I'll only ever see this one subscription.(?) But how does the server keep track of every user's recordID or know when to send it to a specific device?

The problem is I can't get the notification to work. I manually add a record in the dashboard, and I put the other user's recordID as the CKReference. While I'm adding the record, I have the app running in the background on a device under the user's account whom I added as the CKReference in the field. I'd expect the query to trigger and send a push notification since someone commented on this user's post.

Here's my code to setup the subscription:

func subscribe(userID: CKRecordID) {


    let options = CKSubscriptionOptions.FiresOnRecordCreation

    let userRef = CKReference(recordID: userID, action: .DeleteSelf)
    let predicate = NSPredicate(format: "userRef == %@", userRef)
    let predicateTwo = NSPredicate(format: "read = %@", "")
    let compoundPred = NSCompoundPredicate(type: .AndPredicateType, subpredicates: [predicate, predicateTwo])

    let subscription = CKSubscription(recordType: "Notifications", predicate: compoundPred, subscriptionID: subscriptionID,
        options: options)
    subscription.notificationInfo = CKNotificationInfo()
    subscription.notificationInfo.desiredKeys = ["userPost"]
    subscription.notificationInfo.alertBody = "Someone replied to your Pod post"

    publicDB.saveSubscription(subscription, completionHandler: {

        subscription, error in

        if (error != nil) {

            println("error subscribing: \(error)")

        } else {

            println("subscribed!")

        }

    })
}

Upvotes: 3

Views: 648

Answers (1)

harryhorn
harryhorn

Reputation: 912

Let me try and answer:

  1. SubscriptionId allows you to identify later a subscription for example to delete it using the CKDatabase method deleteSubscriptionWithID
  2. For how see answer 1. As to why, well maybe you do not want to get notifications on this subscription any more. This depends on what you are trying to achieve with your app.
  3. Yes
  4. Yes, if you register only one subscription it should work for all users of your app

Regarding your issues, please note that a user recordIDs are special, so you may have issues specifically related to that due to the privacy issues around them. I would suggest to try a simple case that does not involve users and see if subscriptions are working for you. Then think again about how you use user record IDs

Upvotes: 1

Related Questions