I'm trying to devise a sync strategy when using Parse on Mac/iOS. Pushing local changes is easy enough, but I'm unsure on how to fetch the most recent PFObject
changes from Parse.
I'm coming from CloudKit where you can subscribe to record changes. When you save something, your other devices get a background push notification letting them know that a record changed (and what it's CKRecordID
is) so that you can fetch it and update your data.
I'd like to do something similar on Parse. I've been reading through the docs and I don't see anything for this. I know I can query PFObjects
and send push notifications, but I don't see a specific application for sending a push on an object change.
Do I have to set up an afterSave
hook with Cloud Code? From what I've read, I can do a class-level hook like this:
Parse.Cloud.afterSave("Project", function(request) {
//...
})
But is there a way to do this at the object level? Or can I at least include the changed objectId
in the push notification payload somehow?
The answer is Yes. you should use an afterSave (or beforeSave) to trigger a function (in this case, sending push notification) to other clients.
for instance:
Parse.Cloud.afterSave("Project", function(request) {
if (request.original){
// meaning this is an update to the object.
myPushHandler.push({updatedprojectId:request.object.id});
} else {
// the first time this object is created.
}
})
Note: you can also use parse LiveQuery instead of push notification. Parse Live Query