Live Query with Objective-C

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

livequery_ios_app

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

Quickstart

Step 1 – Enable Live Query

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

screenshot4

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

screenshot1

Add the subdomain name you wish to have:

screenshot2

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

screenshot3

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 endpoint.back4app.io" 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:

https://parseplatform.github.io/Parse-SDK-iOS-OSX/api/Classes/PFQuery.html

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:

https://github.com/rvitorper/back4app-livequery-objc