How to connect CKQueryNotification to a CKRecord or CKSubsription?

944 views Asked by At

I need a way to connect a CKQueryNotification to a CKRecord or CKSubscription so I can receive updates/inserts/deletes on multiple record types. I'm successfully receiving CKNotifications and this is the payload:

{
    ck =     {
        ce = 2;
        cid = "<my cloud container id>";
        nid = "<unknown guid>";
        qry =         {
            dbs = 2;
            fo = 1;
            rid = "<the record id>";
            sid = "<THIS IS THE SUBSCRIPTION ID>";
            zid = "_defaultZone";
            zoid = "_defaultOwner";
        };
    };
}

I can get the subscription ID by the notification payload, and I could tie the subscription ID to a local cache that knows the record type, but I want to use a CKFetchNotificationChangesOperation to retrieve unread notifications, and at that point I only have a CKQueryNotification object.

The CKQueryNotification object only has a CKRecordID, and as far as I can tell, I can't get a CKRecord from the CKRecordID. I could perform a query on all of my CKRecord->recordType's in my container but that doesn't seem right.

Any help is appreciated!

1

There are 1 answers

7
Edwin Vermeer On BEST ANSWER

if you receive a push notification, then you should see if you can cast it to a CKQueryNotification. Indeed you should call -[CKDatabase fetchRecordWithID:completionHandler:] for getting the complete record. you can then use the .recordType to see what kind of record it is.

You only have a problem when you have multiple subscriptions for the same recordType. You could solve that by checking if the object complies to the predicate that you used for that subscription. See the predicate.evaluateWithObject method for this. You can not use this if you have a predicate for a CKReference.

If you want a working sample of this, then you could have a look at: https://github.com/evermeer/EVCloudKitDao Which has some other nice features like automatic parsing from and to CKRecord and caching on device of results.