Search code examples
macoscloudkitcksubscriptioncknotification

CloudKit Get actual Object from Record ID in OSX


I am trying to use CKSubscription to subscribe to changes. I am following Apple's docs which seems to be very general and incomplete. Link to Apple Doc

I have got the point of getting Record ID sent to my app via the didReceiveRemoteNotification method in AppDelegate and I have got my Record Id: using this code:

func application(application: NSApplication, didReceiveRemoteNotification userInfo: [String : AnyObject]) {
        let cknNotification = CKNotification(fromRemoteNotificationDictionary: userInfo as! [String:NSObject])

        if cknNotification.notificationType == .Query,
            let queryNotification = cknNotification as? CKQueryNotification {
            let recordId = queryNotification.recordID
            print(recordId)
        }

How do I convert the CKNotification into the Actual Object I stores in Cloudkit? Do I need to perform another fetch or is the data contained in the CKNotification that I just need to Cast.


Solution

  • Looks like I worked it out.

    The Notification only lets you know that they have changed. You need to do the work to pull the new records and update them. ( Makes sense really)

    privateCloudDatabase().fetchRecordWithID(recordId!, completionHandler: { (recordfetched, error) in

                //Check the Record Type if multiple.. I only have one type.
       let myRecordType = recordfetched?.recordType
                let myObject = mySuperObject(record: recordfetched!)
                print("done")
            })
    

    // I should probably add more optional checking - but above just illustrates the solution and how simple it is to get.