Query Cookbook
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.
To complete this tutorial, you will need:
- A basic iOS App to test queries
To explore the different methods theQuery<U> class provide to execute queries.
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
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
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.
With the following snippet we create and save some data to test miscellaneous queries
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.
These methods are responsible for running the query and retrieving its results, being always present in your query implementation.
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.
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
These constraints also work on Date type fields. For instance, we can have
For Bool type fields, we have the Equal and Not equal options. For instance
For fields containing location data (i.e., of type ParseGeoPoint) we have the following query options.
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
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.
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.
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.
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.
These methods will create compound queries, which can combine more than one Query<Profile> instance to achieve more complex results.
These methods are related to the database preferences and operations.
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.