iOS

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 the Query<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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
import ParseSwift
...

struct Profile: ParseObject {
    ...
    
    // Custom properties to organize the person's information
    var name: String?
    var birthday: Date?
    var numberOfFriends: Int?
    var favoriteFoods: [String]?
    var luckyNumbers: [Int]?
    var lastLoginLocation: ParseGeoPoint? // A data type to store coordinates for geolocation
    var isActive: Bool?
    var membership: Pointer<Membership>? // More details about Membership below
    
    ...
}

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

1
2
3
4
5
6
7
8
9
10
11
12
import ParseSwift
...

struct Membership: ParseObject {
    ...
    
    // Custom properties to organize the membership's information
    var typeDescription: String?
    var expirationDate: Date?

    ...
}

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

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
69
70
71
72
73
74
75
76
77
78
79
let adamSandler = Profile(
  name: "Adam Sandler",
  birthday: Date(string: "09/09/1996"),
  numberOfFriends: 2,
  favoriteFoods: ["Lobster", "Bread"],
  luckyNumbers: [2, 7],
  lastLoginLocation: try? ParseGeoPoint(latitude: 37.38412167489413, longitude: -122.01268034622319),
  isActive: false,
  membership: nil
)

let britneySpears = Profile(
  name: "Britney Spears",
  birthday: Date(string: "12/02/1981"),
  numberOfFriends: 52,
  favoriteFoods: ["Cake", "Bread"],
  luckyNumbers: [22, 7],
  lastLoginLocation: try? ParseGeoPoint(latitude: 37.38412167489413, longitude: -122.01268034622319),
  isActive: true,
  membership: nil
)

let carsonKressley = Profile(
  name: "Carson Kressley",
  birthday: Date(string: "03/27/1975"),
  numberOfFriends: 12,
  favoriteFoods: ["Fish", "Cookies"],
  luckyNumbers: [8, 7],
  lastLoginLocation: try? ParseGeoPoint(latitude: 37.38412167489413, longitude: -122.01268034622319),
  isActive: true,
  membership: nil
)

// We create and save a Membership object
let premiumMembership = Membership(typeDescription: "Premium", expirationDate: Date(string: "10/10/2030"))
let savedPremiumMembership = try! premiumMembership.save() // Careful with the forced unwrap

let danAykroyd = Profile(
  name: "Dan Aykroyd",
  birthday: Date(string: "07/01/1952"),
  numberOfFriends: 66,
  favoriteFoods: ["Jam", "Peanut Butter"],
  luckyNumbers: [22, 77],
  lastLoginLocation: try? ParseGeoPoint(latitude: 37.38412167489413, longitude: -122.01268034622319),
  isActive: true,
  membership: try? .init(savedPremiumMembership)
)

let eddyMurphy = Profile(
  name: "Eddie Murphy",
  birthday: Date(string: "04/03/1961"),
  numberOfFriends: 49,
  favoriteFoods: ["Lettuce", "Pepper"],
  luckyNumbers: [6, 5],
  lastLoginLocation: try? ParseGeoPoint(latitude: -27.104919974838154, longitude: -52.61428045237739),
  isActive: false,
  membership: nil
)

let fergie = Profile(
  name: "Fergie",
  birthday: Date(string: "03/27/1975"),
  numberOfFriends: 55,
  favoriteFoods: ["Lobster", "Shrimp"],
  luckyNumbers: [12, 7],
  lastLoginLocation: try? ParseGeoPoint(latitude: -27.104919974838154, longitude: -52.61428045237739),
  isActive: false,
  membership: nil
)

do {
  _ = try? adamSandler.save()
  _ = try? britneySpears.save()        
  _ = try? carsonKressley.save()
  _ = try? danAykroyd.save()
  _ = try? eddyMurphy.save()
} catch {
  // Handle the errors here
}

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.

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.

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

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

1
2
3
4
5
6
7
8
let constraint: QueryConstraint = "birthday" > Date(string: "08/19/1980") // Selects all Profiles who were born after 08/19/1980
let query = Profile.query(constraint)

let profiles = try? query.find() // Executes the query synchronously

query.find { result in // Executes the query asynchronously
  // Handle the result (of type Result<[Profile], ParseError>)
}

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

1
2
3
4
5
6
7
8
let constraint: QueryConstraint = "isActive" == true
let query = Profile.query(constraint) // Selects all active Profiles

let profiles = try? query.find() // Executes the query synchronously

query.find { result in // Executes the query asynchronously
  // Handle the result (of type Result<[Profile], ParseError>)
}

Queries with constraints involving geolocation

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

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

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.

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.

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.

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.

Compound queries

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 MongoDB preferences and operations.

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.