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:
- An App created on Back4App.
- A basic iOS App to test queries
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.
This is the basic method for retrieving your query results
1
2
3
4
5
6
7
let query = Profile.query() // Instantiate a Query<Profile> class with no additional constraints
let profiles = try? query.find() // Executes the query synchronously returns and array of Profiles. It throws an error if something happened
query.find { result in // Executes the query asynchronously
// Handle the result (of type Result<[Profile], ParseError>)
}
Retrieves the number of elemets found by the query
1
2
3
4
5
6
7
let query = Profile.query() // Instantiate a Query<Profile> class with no additional constraints
let profiles = try? query.count() // Executes the query synchronously
query.count { result in // Executes the query asynchronously
// Handle the result (of type Result<Int, ParseError>)
}
Runs the query and returns a list of unique values from the results and the specified field, name
in this case.
1
2
3
4
5
6
7
let query = Profile.query() // Instantiate a Query<Profile> class with no additional constraints
let profiles = try? query.distinct("name") // Executes the query synchronously
query.distinct("name") { result in // Executes the query asynchronously
// Handle the result (of type Result<[Profile], ParseError>)
}
Retrieves a complete list of Profile
’s that satisfy this query.
1
2
3
4
5
6
7
let query = Profile.query()
let profiles = try? query.findAll() // Executes the query synchronously
query.findAll { result in // Executes the query asynchronously
// Handle the result (of type Result<[Profile], ParseError>)
}
Retrieves the first Profile
instance that meets the query criteria.
1
2
3
4
5
6
7
let query = Profile.query()
let profile = try? query.first() // Executes the query synchronously
query.first { result in // Executes the query asynchronously
// Handle the result (of type Result<Profile, ParseError>)
}
When you need to retrieve an object with a specific objectId
1
2
3
4
5
let profile = try? Profile(objectId: "HuYfN4YXFD").fetch() // Retrieves synchronously the Profile with the given objectId. It throws an error if something happened
Profile(objectId: "HuYfN4YXFD").fetch { // Retrieves asynchronously the Profile with the given objectId
// Handle the result (of type Result<Profile, ParseError>)
}
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.
Used when we need to select all the objects which have a field equal to a given String
. We use the ==
operator to construct the corresponding constraint. On the left-hand side goes the field’s name and on the right hand side goes the String
to compare against
1
2
3
4
5
6
7
8
let constraint: QueryConstraint = "name" == "Fergie" // Selects all Profiles with name equals to Fergie
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>)
}
Used when one of the fields satisfy a given regular expression
1
2
3
4
5
6
7
8
let regexCondition: QueryConstraint = matchesRegex(key: "name", regex: "[\\s?a-zA-Z?]?ie[a-zA-Z\\s?]?")
// matchesRegex(key:regex:) is a method provided by the ParseSwift SDK
let query = Profile.query(regexCondition) // Executes the query synchronously
query.find { result in // Executes the query asynchronously
// Handle the result (of type Result<[Profile], ParseError>)
}
Used when one of the fields contains a given String
1
2
3
4
5
6
7
8
9
let constraint: QueryConstraint = containsString(key: "name", substring: "ie")
// containsString(key:substring:) is a method provided by the ParseSwift SDK
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>)
}
Similar to ==
but it has additional options like case sensite, language and diacritic sensitive
1
2
3
4
5
6
7
8
9
10
11
// Careful with the force unwrap!
let constraint: QueryConstraint = try! matchesText(key: "name", text: "fergie", options: [.caseSensitive: false])
// matchesText(key:text:,options:) is a method provided by the ParseSwift SDK
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>)
}
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
Use this constraint when you need to select objects where a certain number type field is equal to a given value.
1
2
3
4
5
6
7
8
let constraint: QueryConstraint = "numberOfFriends" == 2 // Selects all Profiles with number of friends = 2
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>)
}
The opposite constraint of equals to and it also works with Date
type fields.
1
2
3
4
5
6
7
8
let constraint: QueryConstraint = "numberOfFriends" != 2 // Selects all Profiles where the number of friends is different from 2
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>)
}
To construct this constraint we use the <
operator. On the left-hand side goes the field’s name and on the right-hand side goes the value to compare against
1
2
3
4
5
6
7
8
let constraint: QueryConstraint = "numberOfFriends" < 49 // Selects all Profiles with a maximum of 48 friends
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>)
}
To construct this constraint we use the <=
operator. On the left-hand side goes the field’s name (also referred as key) and on the right-hand side goes the value to compare against
1
2
3
4
5
6
7
8
let constraint: QueryConstraint = "numberOfFriends" <= 49 // Selects all Profiles with a maximum of 49 friends
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>)
}
Once again, for this constraint we use another operator to construct it, the >
operator.
1
2
3
4
5
6
7
8
let constraint: QueryConstraint = "birthday" > Date(string: "08/19/1980") // Selects all Profiles who 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>)
}
Last but not least, the greater than or equal constraint are constructed using the >=
operator.
1
2
3
4
5
6
7
8
let constraint: QueryConstraint = "numberOfFriends" >= 20 // Selects all Profiles with at least 20 number of friends
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>)
}
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.
In order to select objects where one of their ParseGeoPoint
type fields is near to another given geopoint
, we use
1
2
3
4
5
6
7
8
9
10
11
12
guard let geopoint = try? ParseGeoPoint(latitude: 37.38412167489413, longitude: -122.01268034622319) else {
return
}
let query = Profile.query(near(key: "lastLoginLocation", geoPoint: geopoint))
// near(key:geoPoint:) is a method provided by the ParseSwift SDK
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>)
}
Given an object with a ParsePolygon
type field, in order to select them in a query depending on what geopoints the field contains, we can use the following constraint
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
struct MyObject: ParseSwift {
...
var aPolygon: ParsePolygon?
...
}
guard let geopoint = try? ParseGeoPoint(latitude: 37.38412167489413, longitude: -122.01268034622319) else {
return
}
let query = MyObject.query(polygonContains(key: "aPolygon", point: geopoint))
// polygonContains(key:point:) is a method provided by the ParseSwift SDK
let myObjects = try? query.find() // Executes the query synchronously
query.find { result in // Executes the query asynchronously
// Handle the result (of type Result<[MyObject], ParseError>)
}
Used to select objects where a ParseGeoPoint
type field is inside of a given geopoint box. This box is described by its northeastPoint
and southwestPoint
geopoints.
1
2
3
4
5
6
7
8
9
10
11
12
13
guard let southwestPoint = try? ParseGeoPoint(latitude: 37.38412167489413, longitude: -122.01268034622319),
let northeastPoint = try? ParseGeoPoint(latitude: 37.28412167489413, longitude: -121.91268034622319) else {
return
}
let query = Profile.query(withinGeoBox(key: "lastLoginLocation", fromSouthWest: southwestPoint, toNortheast: northeastPoint))
// withinGeoBox(key:fromSouthWest:toNortheast:) is a method provided by the ParseSwift SDK
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>)
}
Used to select objects according to whether or not a ParseGeoPoint
type field value is near a given geopoint
. Additionally, we should provide the maximum distance for how far the field value can be from the geopoint.
1
2
3
4
5
6
7
8
9
10
11
guard let geopoint = try? ParseGeoPoint(latitude: 37.38412167489413, longitude: -122.01268034622319) else {
return
}
let query1 = Profile.query(withinKilometers(key: "lastLoginLocation", geoPoint: geopoint, distance: 100))
// withinKilometers(key:geoPoint:distance:) is a method provided by the ParseSwift SDK
let query2 = Profile.query(withinMiles(key: "lastLoginLocation", geoPoint: geopoint, distance: 100))
// withinMiles(key:geoPoint:distance:) is a method provided by the ParseSwift SDK
... // Execute the corresponding query
Used to select objects with a ParseGeoPoint
type field in a given polygon. The vertices of the polygon are represented by ParseGeoPoint
objects.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
// Create the vertices of the polygon (4 in this case)
guard let geopoint1 = try? ParseGeoPoint(latitude: 37.48412167489413, longitude: -122.11268034622319),
let geopoint2 = try? ParseGeoPoint(latitude: 37.48412167489413, longitude: -121.91268034622319),
let geopoint3 = try? ParseGeoPoint(latitude: 37.38412167489413, longitude: -121.91268034622319),
let geopoint4 = try? ParseGeoPoint(latitude: 37.38412167489413, longitude: -122.01268034622319) else {
return
}
let constraint: QueryConstraint = withinPolygon(
key: "lastLoginLocation",
points: [geopoint1, geopoint2, geopoint3, geopoint4]
)
// withinPolygon(key:points:) is a method provided by the ParseSwift SDK
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>)
}
Similar to ‘within kilometers’ or ‘within miles’ but the distance is expressed in radians.
1
2
3
4
5
6
7
8
9
10
11
12
guard let geopoint = try? ParseGeoPoint(latitude: 37.48412167489413, longitude: -122.11268034622319) else {
return
}
let query = Profile.query(withinRadians(key: "lastLoginLocation", geoPoint: geopoint, distance: 1.5))
// withinRadians(key:geoPoint:distance:) is a method provided by the ParseSwift SDK
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 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
Add a constraint to the query that requires a particular field to be contained in the provided array.
1
2
3
4
5
6
7
8
9
10
let constraint: QueryConstraint = containedIn(key: "luckyNumbers", array: [2, 7])
// containedBy(key:array:) is a method provided by the ParseSwift SDK
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>)
}
Add a constraint to the query that requires a particular field to be contained by the provided array. It retrieves objects where all the elements of their array type field match.
1
2
3
4
5
6
7
8
9
10
let constraint: QueryConstraint = containedBy(key: "luckyNumbers", array: [2, 7])
// containedBy(key:array:) is a method provided by the ParseSwift SDK
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>)
}
Add a constraint to the query that requires a particular array type field to contain every element of the provided array.
1
2
3
4
5
6
7
8
9
10
let constraint: QueryConstraint = containsAll(key: "luckyNumbers", array: [2, 7])
// containsAll(key:array:) is a method provided by the ParseSwift SDK
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>)
}
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.
It is used to select objects depending on the value of a given field not being nil
.
1
2
3
4
5
6
7
8
9
10
let constraint: QueryConstraint = exists(key: "membership")
// exists(key:) is a method provided by the ParseSwift SDK
let query = Profile.query(constraint) // Will retrieve all Profiles which have a membership
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>)
}
It is used to select objects depending on the value of a given field being nil
.
1
2
3
4
5
6
7
8
9
10
let constraint: QueryConstraint = doesNotExist(key: "membership")
// doesNotExist(key:) is a method provided by the ParseSwift SDK
let query = Profile.query(constraint) // Will retrieve all Profiles which do not have a membership
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>)
}
When we need to retrieve objects using the result from a different query as a condition, we make use of the method doesNotMatchKeyInQuery(key:queryKey:query:)
to construct the corresponding condition. To illustrate this in more detail, we first introduce a new object Friend
which has a property numberOfContacts
of type Int
1
2
3
4
5
struct Friend: ParseSwift {
...
var numberOfContacts: Int?
...
}
If we want to select all the Profile
objects where their numberOfFriends
field matches exactly with the numberOfContacts
in all Friend
objects, we proceed in the following way
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
let friendQuery = Friend.query() // Selects all Friends objects
let constraint: QueryConstraint = matchesKeyInQuery(
key: "numberOfFriends",
queryKey: "numberOfContacts",
query: friendQuery
)
// matchesKeyInQuery(key:queryKey:query:) is a method provided by the ParseSwift SDK
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>)
}
This kind of composition of queries are commonly referred as multi-object querying.
This case is the opposite of Matches key in query. It is useful when we want to select all the Profile
objects where their numberOfFriends
field does not match with the numberOfContacts
field in all Friend
objects. The method we use to compose this query is doesNotMatchKeyInQuery(key:queryKey:query:)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
let friendQuery = Friend.query() // Selects all Friends objects
let constraint: QueryConstraint = doesNotMatchKeyInQuery(
key: "numberOfFriends",
queryKey: "numberOfContacts",
query: friendQuery
)
// doesNotMatchKeyInQuery(key:queryKey:query:) is a method provided by the ParseSwift SDK
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>)
}
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.
Sort the results in ascending order, overwrites previous orderings. Multiple fields can be used to solve ordering ties. By calling the order(_:)
method on a Query<Profile>
object, we obtain a new query which implements the given order option. The argument for the order(_:)
method is an Array
of Query<Profile>.Order
items. These enumerations contain the name of the field to be used for the sort algorithm. For ascending order we use Query<Profile>.Order.ascending("nameOfTheField")
.
1
2
3
4
5
6
7
let query = Profile.query().order([.ascending("numberOfFriends")])
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>)
}
Similar to the ascending order, the only diference is that instead of using Query<Profile>.Order.ascending("nameOfTheField")
we should use Query<Profile>.Order.descending("nameOfTheField")
.
1
2
3
4
5
6
7
let query = Profile.query().order([.descending("numberOfFriends")])
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>)
}
This kind of sorting procedure relies on a score
metric. In order to use this option, the Profile
object must conform to the ParseQueryScorable
protocol. Once the protocol is implemented, we call the sortByTextScore()
method on the Query<Profile>
object to obtain a new one that implements the required sorting method.
1
2
3
4
5
6
7
8
9
10
11
12
13
extension Profile: ParseQueryScorable {
var score: Double? {
// Add the corresponding implementation
}
}
let query = Profile.query().sortByTextScore()
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>)
}
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.
Calling the exclude(_:)
method on a query returns a new query that will exclude the fields passed (in variadic format) as the argument.
1
2
3
4
5
6
7
let query = Profile.query().exclude("name", "numberOfFriends")
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>)
}
It is used to retrieve the fields whose data type conforms to the ParseObject
protocol (i.e., objects stored in the same Database under its corresponding class name). Calling the include(_:)
method on a query returns a new query that will include the fields passed (in variadic format) as the argument. For instance, the membership
field in Profile
will not be fetched unless we include it manually.
1
2
3
4
5
6
7
let query = Profile.query().include("membership")
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>)
}
When we need to retrieve the fields that conforms to the ParseObject
we call the includeAll()
method on the query.
1
2
3
4
5
6
7
let query = Profile.query().includeAll()
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>)
}
It returns only the specified fields in the returned objects. Calling the select(_:)
method on a query returns a new query which will select only the fields passed (in variadic format) as argument.
1
2
3
4
5
6
7
let query = Profile.query().select("name", "birthday")
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>)
}
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.
Determines the maximum number of results a query is able to return, the default value is 100. We call the limit(_:)
method on the query in question to change this value.
1
2
3
4
5
6
7
let query = Profile.query().limit(2)
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>)
}
Together with the previous option, the skip option allows us to paginate the results. We call the skip(_:)
method on the query in question to change this value.
1
2
3
4
5
6
7
let query = Profile.query().skip(2)
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>)
}
When we need to know in advance the number of results retrieved from a query, we use the withCount(completion:)
method instead of the usual find(...)
. In this way, the result is composed by the objects found together with an integer indicating the length of the array.
1
2
3
4
5
let query = Profile.query()
query.withCount { result in // Executes the query asynchronously
// Handle the result (of type Result<([Profile], Int), ParseError>)
}
Compound queries
These methods will create compound queries, which can combine more than one Query<Profile>
instance to achieve more complex results.
Compose a compound query that is the AND
of the passed queries. This is accomplished by first instantiating the queries to be used for the AND
operation. The and(queries:) method provided by the ParseSwift SDK
allows us to perform the AND
operation and embed the result in a QueryConstraint
object. This constraint is then used to instantiate the final query to be used for retrieveing the results.
1
2
3
4
5
6
7
8
9
10
11
let query1 = Profile.query("numberOfFriends" > 10)
let query2 = Profile.query("numberOfFriends" < 50)
let query = Profile.query(and(queries: query1, query2))
// and(queries:) is a method provided by the ParseSwift SDK. It takes an array of queries and applies the AND op. on them.
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>)
}
In this case, we apply a NOR
operation on a set of queries. We use the nor(queries:)
method provided by the ParseSwift SDK
to perform such operation. We handle the result in a similar way as the AND
operation.
1
2
3
4
5
6
7
8
9
10
let query1 = Profile.query("numberOfFriends" > 10)
let query2 = Profile.query("numberOfFriends" < 50)
let query = Profile.query(nor(queries: query1, query2))
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>)
}
Similar to previous cases, we apply an OR
operation on a set of queries. We use the or(queries:)
method provided by the ParseSwift SDK
to perform such operation. We handle the result in a similar way as the AND
(or NOR
) operation.
1
2
3
4
5
6
7
8
9
10
let query1 = Profile.query("numberOfFriends" > 10)
let query2 = Profile.query("numberOfFriends" < 50)
let query = Profile.query(or(queries: query1, query2))
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>)
}
MongoDB related
These methods are related to MongoDB preferences and operations.
Executes an aggregate query, retrieving objects over a set of input values. To use this feature, instead of executing the query with find(...)
, we call the aggregate(_:completion:)
. The first argument of this function is a dictionary containing the information about the pipeline. For more details, refer to MongoDB documentation on aggregate.
1
2
3
4
5
let query = Profile.query()
query.aggregate([["pipeline": 5]]) { result in
// Handle the result (of type Result<[Profile], ParseError>)
}
Investigates the query execution plan, related to MongoDB explain operation. This option requires a new object (conforming to the Decodable
protocol) to handle the results.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
struct AnyCodable: Decodable {
// Implement the corresponding properties
}
let query = Profile.query()
let results = try? query.findExplain() // Executes the option synchronously
// Instantiate the completion block explicitely in order for the method findExplain(completion:) to infer the type of the returning results
let completion: (Result<[AnyCodable], ParseError>) -> Void = { result in
// Handle the result
}
// Executes the option asynchronously
query.findExplain(completion: completion)
When using a MongoDB replica set, use this method to choose from which replica the objects will be retrieved. The possible values are PRIMARY
(default), PRIMARY_PREFERRED
, SECONDARY
, SECONDARY_PREFERRED
, or NEAREST
.
1
2
3
4
5
6
7
let query = Profile.query().readPreference("NEAREST")
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>)
}
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.