CloudKit subscription notification for CKReference not working as expected

I am trying to set up a CKS subscription for records that contain a CKReference field for a user. But anytime a record is created, it ignores that part of the composite packet and the notification never arrives. Is there something different about using CKReference in a predicate for a CKS subscription? I go to the dashboard to enter a new entry under my own UserIDID of the user (when launching another user in the simulator) because I believe I read that if the entry comes from the device it will not receive a notification. Any insight is greatly appreciated as I've been stuck on this for a week and can't find anything on the internet specific to this. I can get true type predicate notifications, so I think my basic setup is fine.

In the dashboard, I see one shared subscription for both test users, but not for any specific post ID for the user (is this important?):

Notifications.read (equals string)
Notifications.user (equals reference)

      

When I execute the fetchAllSubscriptionsWithCompletionHandler method, it shows the current user post ID for that device as a CKReference in the debugger. So I don't know why it won't work.

Here's my code where I first create a CKReference and then use it for my predicate:

var recordIDString = CKRecordID(recordName: "_a86dac8ee05f8c6ab35746bf9663d7b8")
// I normally store this string in the NSUserDefaults.

    let userRef = CKReference(recordID: recordIDString, action: .DeleteSelf)
     let predicate = NSPredicate(format: "user = %@", userRef)
   let predicateTwo = NSPredicate(format: "read = %@", "")
// I want the read field to be empty, which tells me user has not read my internal app notif.
  let compoundPred = NSCompoundPredicate(type: .AndPredicateType, subpredicates: [predicate, predicateTwo])


Now I set-up the subscription like this:

let subscription = CKSubscription(recordType: "Notifications",
        predicate: compoundPred,
        options: .FiresOnRecordCreation)

    let notificationInfo = CKNotificationInfo()

    notificationInfo.alertBody = "Someone replied to your post"
    notificationInfo.shouldBadge = true

    subscription.notificationInfo = notificationInfo

    publicDB.saveSubscription(subscription,
        completionHandler: ({subscription, error in.....etc})
      // handling this part

      

+2


source to share


2 answers


I had this same problem and came across this information on the apples website:

When creating a subscription whose predicate contains a CKReference field, be sure to use the CKRecordID object as the value for the field when creating the predicate. Using the CKRecord object does not currently work in this case.



When I changed my predicate for my CKQuerySubscription to use CKRecordID instead of CKReference, it worked as expected. I realize this is an old thread, but I was struggling to find an answer to this problem as the predicate seems to behave differently between CKQuery and CKQuerySubscription.

+3


source


Initialize your subscription NSPredicate

instead NSCompoundPredicate

:

let predicate = NSPredicate(format: "user = %@ AND read = %@",
    argumentArray: [userRef, ""])
let subscription = CKSubscription(recordType: "Notifications",
    predicate: predicate,
    options: .FiresOnRecordCreation)

      



In my experience with CloudKit, this should work.

+1


source







All Articles