Struggle to understand CKS subscriptions in CloudKit

I have been reading and trying to use the CKSubscription feature for several weeks, but I cannot find information on some common questions. I've read Apple docs, online tutorials, books I've bought and questions here on SO, but I still don't understand the underlying principles I think. Any help is greatly appreciated.

Here are the questions that I cannot find answers to:

1). What is the purpose of a subscription ID? The convenience installer doesn't include it, so why is it needed in the designated init? If you use it, will it be the same for all users of the application?

2). I saw someone mention here that you can unregister the subscription id. Why and how will you do it?

3). Can subscriptions be configured in both the public and private user database?

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

For example, I'm having trouble getting notifications to work with my specific use case. This is not a problem in my setup, as I can get the True predicate to work and get notified. So I don't have to understand the basics of how subscriptions work.

I'm trying to set up a subscription that fires whenever a new post is created, when another user comments on a post. This new post will then contain a link to the person who posted the post. The only subscription I see in the database for both users is Notifications.user (equal to the link). So, I assume that I will only see this subscription. (?) But how does the server keep track of each custom post ID, or does it know when to send it to a specific device?

The problem is I can't get the notification to work. I am manually adding an entry to the dashboard and I am putting a different user id as the CKReference. While I add the entry, I have an app running in the background on the device under a user account, which I added as a CKReference to the field. I expect the request to trigger and send a push notification since someone has commented on this user post.

Here's my code for setting up a 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!")

        }

    })
}

      

+3


source to share


1 answer


Let me try to answer:

  • SubscriptionId allows you to identify the subscription later, for example to delete it using the CKDatabase method deleteSubscriptionWithID
  • How to find out the answer 1. Regarding why, you may not want to receive notifications for this subscription. It depends on what you are trying to achieve with your application.
  • Yes
  • Yes, if you only register one subscription, it should work for all users of your application.


As far as your concerns go, please note that custom post IDs are special, so you may have issues with this due to privacy concerns. I would suggest trying a simple case that doesn't include users and see if subscriptions work for you. Then think about how you use custom post IDs.

+1


source







All Articles