Parse Live Query on Symbyoz

Last updated 3 months ago

PFQuery is one of the key concepts for Parse. It allows you to retrieves PFObject by specifying some conditions, making it easy to build apps such as a dashboard, a todo list or even some strategy games. However, PFQuery is based on a PULL model, which is not suitable for apps that need real-time support.

Suppose you are building an app that allows multiple users to edit the same file at the same time like a Chat. PFQuery would not be an ideal tool since you can not know when to query from the server to get the updates.

To solve this problem, we introduce Parse LiveQuery. This tool allows you to subscribe to a PFQuery you are interested in. Once subscribed, the server will notify clients whenever a PFObject that matches the PFQuery is created or updated, in real-time.

Live Query is a PUSH model.

Java
public void getLiveMessages()
{
ParseLiveQueryClient parseLiveQueryClient = ParseLiveQueryClient.Factory.getClient();
ParseQuery<ParseObject> queryMessage = ParseQuery.getQuery("Messages");
SubscriptionHandling<ParseObject> messageChannel = parseLiveQueryClient.subscribe(queryMessage);
// CHOOS ONLY ONE BETWEEN handleEvent and handleEvents
messageChannel.handleEvents(new SubscriptionHandling.HandleEventsCallback<ParseObject>()
{
@Override
public void onEvents(ParseQuery<ParseObject> query, SubscriptionHandling.Event event, ParseObject object)
{
// Triggered when an object from the class Messages is created, updated or deleted
// use method handleEvent instead of handleEvents to get ONLY created object
}
});
}