1

I use CloudKit. I have a Channel record type which does have isLive as an attribute. It can be either 0 or 1 indicating if the channel is live.

I want to send a notification to all users if the live status if a channel changes from 0 to 1.

I tried to define the CKQuerySubscription like this:

    let liveSubscription = CKQuerySubscription(recordType: "Channel", predicate: NSPredicate(format: "(recordID == '4cebe441-c6f7-47af-85df-e6572e757c5c') AND (isLive == 1)"), options: [.firesOnRecordUpdate])
    let liveNotificationInfo = CKNotificationInfo()
    liveNotificationInfo.shouldSendContentAvailable = false
    liveNotificationInfo.shouldBadge = true
    liveNotificationInfo.alertBody = "Test is live now"
    channelSubscription.notificationInfo = liveNotificationInfo

This does send a notification on any update of this channel record though. It does not care about the isLive predicate.

Any idea how can I receive notifications only for isLive change from 0 to 1?

Or is it better to receive all updates on this channel record, process them in my app and then send a local notification if necessary?

Bartłomiej Semańczyk
  • 59,234
  • 49
  • 233
  • 358
funkenstrahlen
  • 3,032
  • 2
  • 28
  • 40

2 Answers2

0

There is no possibility to register for changes only on one attribute update. You will get the notification always when ANY of fields is updated. This is much better and it works always like this.

Bartłomiej Semańczyk
  • 59,234
  • 49
  • 233
  • 358
0

Two things. First, you should be sending a reference to the record rather than the raw recordName string. Second, add .firesOnce to the subscription so it only fires once.

            let reference = CKReference(recordID: recordID, action: .none)
            let predicate = NSPredicate(format: "%K == %@ && %K == %@",
                                        "isLive", 1,
                                        "recordID", reference)
            let liveSubscription = CKQuerySubscription(recordType: "Channel",
                                                       predicate: predicate,
                                                       options: [.firesOnRecordUpdate, .firesOnce])
John Scalo
  • 3,194
  • 1
  • 27
  • 35