Live Query with Objective-C

After this tutorial, you’ll be able to build a user system like this one:


Before going into the action, make sure you have a working project. You can check out the links below to the Quickstart guide:


Step 1 – Enable Live Query

To enable the Live Query, just go to your Back4app App page and find this option:


Click to see more options and tick a checkbox with label “Activate Hosting”:


Add the subdomain name you wish to have:


Then Activate Live Query and select the classes you want to enable the Live Query:


You are all set with the server! Now, to the code.

Step 2 – Setting up the LiveQuery client on iOS

Parse Server’s github offers a nice implementation of Live Query functionality that can be included in your project in an easy way. We’ll need to use CocoaPods for that. First of all, you’ll need to setup CocoaPods. Google has a nice video on that:

After you have a working CocoaPods setup, you’ll need to add this line to your target on the Podfile:

pod 'ParseLiveQuery'

The Objective-C version of the Live Query uses a Swift implementation that is compatible with Objective-C. Hence, this implementation uses Swift and requires the ‘use_frameworks!’ option to work properly with CocoaPods. Then, we have to import the header properly, which will be done in the ‘ViewController.h’:

#import "ParseLiveQuery-Swift.h"

And you’re set to use the Live Query API. Next, it is imperative to subscribe and listen to events.

Step 3 – Subscribing and listening to events

We have to create a few fields in the interface that will use such service. Make sure they are a property of that interface, as it is necessary to keep its memory allocated. We will define three objects in the ‘ViewController.m’, inside the ‘@interface ViewController()’ scope:

@property (nonatomic, strong) PFLiveQueryClient *liveQueryClient;
@property (nonatomic, strong) PFQuery *msgQuery;
@property (nonatomic, strong) PFLiveQuerySubscription *subscription;

After that, it is necessary to instantiate each of the objects. This part will be done at the ‘viewDidLoad’ method of the ‘ViewController’. The ‘PFQuery’ object has to have a filter set up, as if not, it won’t work. The instantiation of the ‘PFLiveQueryClient’ works like:

self.liveQueryClient = [[PFLiveQueryClient alloc] initWithServer:@"wss://your API" applicationId:@"Your App ID" clientKey:@"Your Client Key"];

The ‘PFQuery’ will be like this:

self.msgQuery = [PFQuery queryWithClassName:@"Message"];

[self.msgQuery whereKey:@"destination" equalTo:@"pokelist"];

And the ‘PFLiveQuerySubscription’ as follows:

self.subscription = [self.liveQueryClient subscribeToQuery:self.msgQuery];

[self.subscription addCreateHandler:^(PFQuery<PFObject *> * _Nonnull query, PFObject * _Nonnull object) {
    // The actions to handle the object that is incoming go here

Note that the filter setup for the ‘msgQuery’ has many options available, as described in the Parse PFQuery documentation:

Also, the only event that has been shown in the example above is the ‘Create’ event. To use other options, it is possible to define other handlers, such as ‘addDeleteHandler’ and ‘addUpdateHandler’, using the very same syntax used above.

Now, we are able to send messages.

Step 4 – Sending messages

It is pretty straightforward to send messages. You only have to create a ‘PFObject’ object and save it in background, as in this example:

PFObject* poke = [PFObject objectWithClassName:@"Message"];

poke[@"content"] = @"poke";
poke[@"destination"] = @"pokelist";

[poke saveInBackgroundWithBlock:^(BOOL succeeded, NSError * _Nullable error) {
    // Do something after the message has been sent here

And that’s it. Your real-time app is working. Check out the full source code below: