cloudkit notifications for a specific field within a record

385 views Asked by At

I have cloudkit notifications working. When someone changes the record, the subscribers are notified. My subscription definition looks like:

         NSPredicate *searchConditions = [NSPredicate predicateWithFormat:@"%K = %@", CLOUDKIT_PUBLIC_ID_GUID, theCloudGUID];
         int subscriptionOptions = CKSubscriptionOptionsFiresOnRecordUpdate | CKSubscriptionOptionsFiresOnRecordDeletion;
         CKSubscription *publicSubscription = [[CKSubscription alloc] initWithRecordType:CLOUDKIT_RECORDNAME_PUBLIC_ID
                                                                               predicate:searchConditions
                                                                                 options:subscriptionOptions];

         CKNotificationInfo *notificationInfo = [CKNotificationInfo new];
         notificationInfo.alertLocalizationKey = CLOUDKIT_NOTIFICATION_PUBLIC;
         notificationInfo.shouldBadge = NO;
         notificationInfo.alertBody = CLOUDKIT_NOTIFICATIONBODY_PUBLIC;
         publicSubscription.notificationInfo = notificationInfo;

         [publicDatabase saveSubscription:publicSubscription completionHandler:^(CKSubscription * _Nullable subscription, NSError * _Nullable error)
          {
              //error handling
          }

The thing is, there are multiple fields in this record. I only want to alert the subscriber when one specific field changes.

When creating the subscription, is there a way to set the search predicate to detect a change in a specific field? I read through the various Predicate docs, but didn't see this specifically mentioned.

Or, when receiving the notification, is there a way to see which fields changed? In didReceiveRemoteNotification I tried:

 CKQueryNotification *queryNotification = [CKQueryNotification notificationFromRemoteNotificationDictionary:userInfo]; 

But queryNotification.recordFields is null.

As a worst case, I have considered breaking the specific field out into it's own record, but then I have the overhead of maintaining more records tied together by a common GUID. I was hoping to keep this more compact.

1

There are 1 answers

1
Clifton Labrum On

Your question is aging a bit, so maybe you already figured this out, but using the desiredKeys property may help: https://developer.apple.com/documentation/cloudkit/cknotificationinfo/1514931-desiredkeys

If these notifications are silent pushes, you could look at the payload to see if certain keys changed and have your app react accordingly.

If these are push (visible-to-the-user) notifications, I don't think you can push based on the key changing per se. You could set an NSPredicate on your CKQuerySubscription if you were testing how the value changed (is it equal to this or not equal to that, etc.), but I'm not sure about it being triggered for any change at all.