Android

Parse Query Cookbook in Android

Introduction

We’ve already seen how a ParseQuery with get can retrieve a single ParseQuery from Back4App. There are many other ways to retrieve data with ParseQuery you can retrieve many objects at once, use conditions on the objects you wish to retrieve, and more.

In this guide, you will ding deep into the ParseQuery class and see all the methods you can use to build your Queries. You will use a simple database class with some mocked data to perform the Queries using the Javascript Console on Back4App.

You can use Javascript Console on Back4App to create mocked data easily, or you can create your data with your own android app by following this guide.

This tutorial uses an app created in Android Studio 4.1.1 with buildToolsVersion=30.0.2 , Compile SDK Version = 30.0.2 and targetSdkVersion=30

At any time, you can access the complete Project via our GitHub repositories.

Prerequisites

To complete this tutorial, we need:

Goal

Explore the ParseQuery class different methods and learn query types you can create on Android.

The ParseQuery class

Any query operation on Parse uses the ParseQuery object type, which will help you retrieve specific data from your Back4App throughout your app. To create a new ParseQuery, you need to pass as a parameter the desired ParseQuery subclass, which is the one that will contain your query results.

It is crucial to know that a ParseQuery will only resolve after calling a retrieve method (like ParseQuery.find or ParseQuery.get), so a query can be set up and several modifiers can be chained before actually being called.

You can read more about the ParseQuery class here at the official documentation.

Using the JS Console on Back4App

Inside your Back4App application’s dashboard, you will find a very useful API console in which you can run JavaScript code directly. In this guide you will use to store and query data objects from Back4App. On your App main dashboard go to Core->API Console->JS Console.

React Native Back4App

Save your Data Objects

To run the queries on this guide you’ll need first to populate your App with some data. Let’s create a sample class called Profile, which mocks a social media profile class using famous people names and the following fields:

  • string type name;
  • Date type birthDay;
  • Number (integer) type friendCount;
  • Array (string array) type favoriteFoods;
  • Array (Number array) type luckyNumbers;
  • GeoPoint type lastLoginLocation;
  • Nullable pointer type premiumMembership, related to a Membership class containing string name and Date expirationDate fields.

Here is the Parse.Object classes creation code, so go ahead and run it in your API console:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
// Add Profile objects and create table
// Adam Sandler
let Profile = new Parse.Object('Profile');
Profile.set('name', 'Adam Sandler');
Profile.set('birthDay', new Date('09/09/1966'));
Profile.set('friendCount', 2);
Profile.set('favoriteFoods', ['Lobster', 'Bread']);
Profile.set('luckyNumbers', [2, 7]);
Profile.set('lastLoginLocation', new Parse.GeoPoint(37.38412167489413, -122.01268034622319));
await Profile.save();

// Britney Spears
Profile = new Parse.Object('Profile');
Profile.set('name', 'Britney Spears');
Profile.set('birthDay', new Date('12/02/1981'));
Profile.set('friendCount', 52);
Profile.set('favoriteFoods', ['Cake', 'Bread']);
Profile.set('luckyNumbers', [22, 7]);
Profile.set('lastLoginLocation', new Parse.GeoPoint(37.38412167489413, -122.01268034622319));
await Profile.save();

// Carson Kressley
Profile = new Parse.Object('Profile');
Profile.set('name', 'Carson Kressley');
Profile.set('birthDay', new Date('11/11/1969'));
Profile.set('friendCount', 12);
Profile.set('favoriteFoods', ['Fish', 'Cookies']);
Profile.set('luckyNumbers', [8, 2]);
Profile.set('lastLoginLocation', new Parse.GeoPoint(37.38412167489413, -122.01268034622319));
await Profile.save();

// Dan Aykroyd
// Creates related object Membership for this user only
let Membership = new Parse.Object('Membership');
Membership.set('name', 'Premium');
Membership.set('expirationDate', new Date('10/10/2030'))
await Membership.save();
Profile = new Parse.Object('Profile');
Profile.set('name', 'Dan Aykroyd');
Profile.set('birthDay', new Date('07/01/1952'));
Profile.set('friendCount', 66);
Profile.set('favoriteFoods', ['Jam', 'Peanut Butter']);
Profile.set('luckyNumbers', [22, 77]);
Profile.set('lastLoginLocation', new Parse.GeoPoint(37.38412167489413, -122.01268034622319));
Profile.set('premiumMembership', Membership);
await Profile.save();

// Eddie Murphy
Profile = new Parse.Object('Profile');
Profile.set('name', 'Eddie Murphy');
Profile.set('birthDay', new Date('04/03/1961'));
Profile.set('friendCount', 49);
Profile.set('favoriteFoods', ['Lettuce', 'Pepper']);
Profile.set('luckyNumbers', [6, 5]);
Profile.set('lastLoginLocation', new Parse.GeoPoint(-27.104919974838154, -52.61428045237739));
await Profile.save();

// Fergie
Profile = new Parse.Object('Profile');
Profile.set('name', 'Fergie');
Profile.set('birthDay', new Date('03/27/1975'));
Profile.set('friendCount', 55);
Profile.set('favoriteFoods', ['Lobster', 'Shrimp']);
Profile.set('luckyNumbers', [13, 7]);
Profile.set('lastLoginLocation', new Parse.GeoPoint(-27.104919974838154, -52.61428045237739));
await Profile.save();

console.log('Success!');

After running this code, you should now have a Profile class in your database with six objects created. Your new class should look like this:

React Native Back4App

Let’s now take a look at examples from every ParseQuery method, along with brief explanations on what they do. Please note that some methods in this list can take options as an additional argument, but in most cases, it is only related to masterKey usage and not relevant to this guide content, so this possibility will be omitted whenever not relevant.

Query retrievers

These methods are responsible for running the query and retrieving its results, being always present in your query implementation.

This is the java methods:

This is the kotlin methods:

Query conditioners

These methods give you the possibility of applying conditional constraints to your query, which are arguably the most important operations in querying. Remember that these operations can all be chained before the results are retrieved, so many combinations can be achieved to solve your querying needs.

These are Java methods

These are Kotlin methods

These are Java methods

These are Kotlin methods

These are Java methods

These are Kotlin methods

Query ordering

Essential in most queries, ordering can be easily achieved in Parse and even chained between two or more ordering constraints.

These are Java methods

These are Kotlin methods

Field selecting

These methods affect which field values can be in your query results.

These are Java methods

These are Kotlin methods

Geopoint querying

These are methods specific to GeoPoint querying.

These are Java Methods

These are Kotlin Methods

Pagination

These methods are related to pagination utilities, useful for queries that will retrieve a large number of results.

These are Java methods

These are Kotlin methods

Local datastore

These methods enable selecting the source of the queries and using a local datastore.

These are Java methods

These are Kotlin methods

Conclusion

At the end of this guide, you learned how to use different query types in Android.