iOS
...
Parse Swift SDK
Data Objects

Query Cookbook

20min

Query Cookbook for ParseSwift

Introduction

In the basic queries guide, we introduced some of the basic methods to construct queries and retrieve data from a Back4App Database. The ParseSwift SDK offers practical ways to construct more complex queries for real-world applications.

In this guide, you will dive deep into the Query<U> (generic) class and see all the methods you can use to build your queries. You will use a simple database class (named Profile) with some mocked data to perform queries using swift.

Before getting started, we should establish a couple of concepts about objects and data types. In this guide, we refer as objects to any data type (like structs) that can be stored in a database. In some situations, they are also referred as items or elements. Properties are the members of a data type (mostly structs). However, in here, we usually call them fields. ParseSwift uses the term ‘key’, to indicate the properties’ name. Thus, any parameter labeled as ‘key’ in any method from the ParseSwift SDK indicates that its value has to match a field’s name.

Prerequisites

To complete this tutorial, you will need:

Goal

To explore the different methods theQuery<U> class provide to execute queries.

The Query<U> class

For this guide, we will query information about a person’s contact. These data is stored under the class Profile on the Back4App Database. In order to retrieve profiles via an iOS App, we need to create the Profile struct that will contain such information

Swift


Additionally, we use the object Membership to show how queries between Profile and Membership interact with each other. The Membership struct is implemented in the following way

Swift


Any query operation on a Back4App Database is performed by the generic class Query<Profile> where the generic type Profile (conforming to the ParseSwift protocol) is the object we are trying to retrieve. After instantiating a Query<Profile> object, we must call one of its find(_) methods (see the basic queries guide) to handle the result returned from the query.

You can read more about the Query<U> class here at the official documentation.

Saving Data Objects

With the following snippet we create and save some data to test miscellaneous queries

Swift


Once the above code is executed, we should have a small database to work with.

See more about how to save data with ParseSwift at iOS Data Objects.

Query retrievers

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

Find
First
Count
Distinct
Find all
Using the objectId


Queries with constraints on string type fields

We now start applying specific constraints to queries. Firstly, we will impose constraints that select objects only when a String type field satisfies a given condition. This and any other type of constraint imposed on a Query<Profile> object is done via the QueryConstraint object. Below and in the next sections we detail how to construct such constraints.

Equal
Regular expression
Contains substring
Matches text


Queries with constraints on comparable-like fields

It is very common to try to select objects where a certain number type field has to be equal, distinct, larger or smaller than a given value. This is accomplished by using the following comparable operations

Equal
Not equal
Less than
Less than or equal
Greater than
Greater than or equal


These constraints also work on Date type fields. For instance, we can have

Swift


For Bool type fields, we have the Equal and Not equal options. For instance

Swift


Queries with constraints involving geolocation

For fields containing location data (i.e., of type ParseGeoPoint) we have the following query options.

Near
Contains
Within geo box
Within km/mph
Within Polygon
Within radians


Queries with constraints involving array type fields

When the objects we want to retrieve need to satisfy a given condition on any of their array type fields, ParseSwift SDK provides the following alternatives to accomplish that

Contained in
Contained by
Contains all


Advanced queries

Until now we introduced the main conditions we can apply on a given field to select objects from a Back4App Database using ParseSwift SDK. In this section we continue with the composition of queries and advanced queries.

Exists
Does not exists
Matches key in query
Does not match key in query


Query ordering

Sorting the results from a query is key before starting to display or manipulate those results. The ParseSwift SDK provides the following options to accomplish that.

Ascending
Descending
Text Score


Field selecting

Depending on what data is required during a query, this can take more or less time. In some scenarios it may be enough to retrieve certain fields from an object and ignore the unnecessary fields. Furthermore, by selecting only the fields we need from a query, we avoid over-fetching and improve performance on the fetching process.

Exclude
Include
Include all
Select


Pagination

In large Databases pagination, is a fundamental feature for querying and handling a large amount of results. the ParseSwift SDK provides the following methods to manage those situations.

Limit
Skip
With count


Compound queries

These methods will create compound queries, which can combine more than one Query<Profile> instance to achieve more complex results.

And
Nor
Or


Database related

These methods are related to the database preferences and operations.

aggregate
explain
readPreference


Conclusion

Using the different methods, objects and operators provided by the ParseSwift SDK, we were able to understand how to construct and retrieve objects from a Back4App Database. With this cookbook you should be able to perform queries with very flexible constraints and manage the results according to your use case. For more details about any of the above topics, you can refer to the ParseSwift repository.