iOS
...
Data Objects
Query Cookbook
20 min
query cookbook for parseswift introduction in the basic queries https //www back4app com/docs/ios/parse swift sdk/data objects/basic queries guide, we introduced some of the basic methods to construct queries and retrieve data from a back4app database the parseswift sdk 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> 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 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 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> 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 profile on the back4app database in order to retrieve profiles via an ios app, we need to create the profile profile struct that will contain such information 1 import parseswift 2 3 4 struct profile parseobject { 5 6 7 // custom properties to organize the person's information 8 var name string? 9 var birthday date? 10 var numberoffriends int? 11 var favoritefoods \[string]? 12 var luckynumbers \[int]? 13 var lastloginlocation parsegeopoint? // a data type to store coordinates for geolocation 14 var isactive bool? 15 var membership pointer\<membership>? // more details about membership below 16 17 18 } additionally, we use the object membership membership to show how queries between profile profile and membership membership interact with each other the membership membership struct is implemented in the following way 1 import parseswift 2 3 4 struct membership parseobject { 5 6 7 // custom properties to organize the membership's information 8 var typedescription string? 9 var expirationdate date? 10 11 12 } any query operation on a back4app database is performed by the generic class query\<profile> query\<profile> where the generic type profile profile (conforming to the parseswift parseswift protocol) is the object we are trying to retrieve after instantiating a query\<profile> query\<profile> object, we must call one of its find( ) find( ) methods (see the basic queries guide https //www back4app com/docs/ios/parse swift sdk/data objects/basic queries ) to handle the result returned from the query you can read more about the query\<u> query\<u> class here at the official documentation https //github com/parse community/parse swift saving data objects with the following snippet we create and save some data to test miscellaneous queries 1 let adamsandler = profile( 2 name "adam sandler", 3 birthday date(string "09/09/1996"), 4 numberoffriends 2, 5 favoritefoods \["lobster", "bread"], 6 luckynumbers \[2, 7], 7 lastloginlocation try? parsegeopoint(latitude 37 38412167489413, longitude 122 01268034622319), 8 isactive false, 9 membership nil 10 ) 11 12 let britneyspears = profile( 13 name "britney spears", 14 birthday date(string "12/02/1981"), 15 numberoffriends 52, 16 favoritefoods \["cake", "bread"], 17 luckynumbers \[22, 7], 18 lastloginlocation try? parsegeopoint(latitude 37 38412167489413, longitude 122 01268034622319), 19 isactive true, 20 membership nil 21 ) 22 23 let carsonkressley = profile( 24 name "carson kressley", 25 birthday date(string "03/27/1975"), 26 numberoffriends 12, 27 favoritefoods \["fish", "cookies"], 28 luckynumbers \[8, 7], 29 lastloginlocation try? parsegeopoint(latitude 37 38412167489413, longitude 122 01268034622319), 30 isactive true, 31 membership nil 32 ) 33 34 // we create and save a membership object 35 let premiummembership = membership(typedescription "premium", expirationdate date(string "10/10/2030")) 36 let savedpremiummembership = try! premiummembership save() // careful with the forced unwrap 37 38 let danaykroyd = profile( 39 name "dan aykroyd", 40 birthday date(string "07/01/1952"), 41 numberoffriends 66, 42 favoritefoods \["jam", "peanut butter"], 43 luckynumbers \[22, 77], 44 lastloginlocation try? parsegeopoint(latitude 37 38412167489413, longitude 122 01268034622319), 45 isactive true, 46 membership try? init(savedpremiummembership) 47 ) 48 49 let eddymurphy = profile( 50 name "eddie murphy", 51 birthday date(string "04/03/1961"), 52 numberoffriends 49, 53 favoritefoods \["lettuce", "pepper"], 54 luckynumbers \[6, 5], 55 lastloginlocation try? parsegeopoint(latitude 27 104919974838154, longitude 52 61428045237739), 56 isactive false, 57 membership nil 58 ) 59 60 let fergie = profile( 61 name "fergie", 62 birthday date(string "03/27/1975"), 63 numberoffriends 55, 64 favoritefoods \["lobster", "shrimp"], 65 luckynumbers \[12, 7], 66 lastloginlocation try? parsegeopoint(latitude 27 104919974838154, longitude 52 61428045237739), 67 isactive false, 68 membership nil 69 ) 70 71 do { 72 = try? adamsandler save() 73 = try? britneyspears save() 74 = try? carsonkressley save() 75 = try? danaykroyd save() 76 = try? eddymurphy save() 77 } catch { 78 // handle the errors here 79 } 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 //this is the basic method for retrieving your query results 1 let query = profile query() // instantiate a query\<profile> class with no additional constraints 2 3 let profiles = try? query find() // executes the query synchronously returns and array of profiles it throws an error if something happened 4 5 query find { result in // executes the query asynchronously 6 // handle the result (of type result<\[profile], parseerror>) 7 } first //retrieves the first profile instance that meets the query criteria 1 let query = profile query() 2 3 let profile = try? query first() // executes the query synchronously 4 5 query first { result in // executes the query asynchronously 6 // handle the result (of type result\<profile, parseerror>) 7 } count //retrieves the number of elemets found by the query 1 let query = profile query() // instantiate a query\<profile> class with no additional constraints 2 3 let profiles = try? query count() // executes the query synchronously 4 5 query count { result in // executes the query asynchronously 6 // handle the result (of type result\<int, parseerror>) 7 } distinct //runs the query and returns a list of unique values from the results and the specified field, name in this case 1 let query = profile query() // instantiate a query\<profile> class with no additional constraints 2 3 let profiles = try? query distinct("name") // executes the query synchronously 4 5 query distinct("name") { result in // executes the query asynchronously 6 // handle the result (of type result<\[profile], parseerror>) 7 } find all //retrieves a complete list of profile’s that satisfy this query 1 let query = profile query() 2 3 let profiles = try? query findall() // executes the query synchronously 4 5 query findall { result in // executes the query asynchronously 6 // handle the result (of type result<\[profile], parseerror>) 7 } using the objectid //when you need to retrieve an object with a specific objectid 1 let profile = try? profile(objectid "huyfn4yxfd") fetch() // retrieves synchronously the profile with the given objectid it throws an error if something happened 2 3 profile(objectid "huyfn4yxfd") fetch { // retrieves asynchronously the profile with the given objectid 4 // handle the result (of type result\<profile, parseerror>) 5 } 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 string type field satisfies a given condition this and any other type of constraint imposed on a query\<profile> query\<profile> object is done via the queryconstraint queryconstraint object below and in the next sections we detail how to construct such constraints equal //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 let constraint queryconstraint = "name" == "fergie" // selects all profiles with name equals to fergie 2 let query = profile query(constraint) 3 4 let profiles = try? query find() // executes the query synchronously 5 6 query find { result in // executes the query asynchronously 7 // handle the result (of type result<\[profile], parseerror>) 8 } regular expression //used when one of the fields contains a given string 1 let constraint queryconstraint = containsstring(key "name", substring "ie") 2 // containsstring(key\ substring ) is a method provided by the parseswift sdk 3 let query = profile query(constraint) 4 5 let profiles = try? query find() // executes the query synchronously 6 7 query find { result in // executes the query asynchronously 8 // handle the result (of type result<\[profile], parseerror>) 9 } contains substring //retrieves the number of elemets found by the query 1 let query = profile query() // instantiate a query\<profile> class with no additional constraints 2 3 let profiles = try? query count() // executes the query synchronously 4 5 query count { result in // executes the query asynchronously 6 // handle the result (of type result\<int, parseerror>) 7 } matches text //similar to == but it has additional options like case sensite, language and diacritic sensitive 1 // careful with the force unwrap! 2 let constraint queryconstraint = try! matchestext(key "name", text "fergie", options \[ casesensitive false]) 3 // matchestext(key\ text ,options ) is a method provided by the parseswift sdk 4 5 let query = profile query(constraint) 6 7 let profiles = try? query find() // executes the query synchronously 8 9 query find { result in // executes the query asynchronously 10 // handle the result (of type result<\[profile], parseerror>) 11 } 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 //use this constraint when you need to select objects where a certain number type field is equal to a given value 1 let constraint queryconstraint = "numberoffriends" == 2 // selects all profiles with number of friends = 2 2 let query = profile query(constraint) 3 4 let profiles = try? query find() // executes the query synchronously 5 6 query find { result in // executes the query asynchronously 7 // handle the result (of type result<\[profile], parseerror>) 8 } not equal //the opposite constraint of equals to and it also works with date type fields 1 let constraint queryconstraint = "numberoffriends" != 2 // selects all profiles where the number of friends is different from 2 2 let query = profile query(constraint) 3 4 let profiles = try? query find() // executes the query synchronously 5 6 query find { result in // executes the query asynchronously 7 // handle the result (of type result<\[profile], parseerror>) 8 } less than //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 let constraint queryconstraint = "numberoffriends" < 49 // selects all profiles with a maximum of 48 friends 2 let query = profile query(constraint) 3 4 let profiles = try? query find() // executes the query synchronously 5 6 query find { result in // executes the query asynchronously 7 // handle the result (of type result<\[profile], parseerror>) 8 } less than or equal //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 let constraint queryconstraint = "numberoffriends" <= 49 // selects all profiles with a maximum of 49 friends 2 let query = profile query(constraint) 3 4 let profiles = try? query find() // executes the query synchronously 5 6 query find { result in // executes the query asynchronously 7 // handle the result (of type result<\[profile], parseerror>) 8 } greater than //once again, for this constraint we use another operator to construct it, the > operator 1 let constraint queryconstraint = "birthday" > date(string "08/19/1980") // selects all profiles who born after 08/19/1980 2 let query = profile query(constraint) 3 4 let profiles = try? query find() // executes the query synchronously 5 6 query find { result in // executes the query asynchronously 7 // handle the result (of type result<\[profile], parseerror>) 8 } greater than or equal //last but not least, the greater than or equal constraint are constructed using the >= operator 1 let constraint queryconstraint = "numberoffriends" >= 20 // selects all profiles with at least 20 number of friends 2 let query = profile query(constraint) 3 4 let profiles = try? query find() // executes the query synchronously 5 6 query find { result in // executes the query asynchronously 7 // handle the result (of type result<\[profile], parseerror>) 8 } these constraints also work on date date type fields for instance, we can have 1 let constraint queryconstraint = "birthday" > date(string "08/19/1980") // selects all profiles who were born after 08/19/1980 2 let query = profile query(constraint) 3 4 let profiles = try? query find() // executes the query synchronously 5 6 query find { result in // executes the query asynchronously 7 // handle the result (of type result<\[profile], parseerror>) 8 } for bool bool type fields, we have the equal and not equal options for instance 1 let constraint queryconstraint = "isactive" == true 2 let query = profile query(constraint) // selects all active profiles 3 4 let profiles = try? query find() // executes the query synchronously 5 6 query find { result in // executes the query asynchronously 7 // handle the result (of type result<\[profile], parseerror>) 8 } queries with constraints involving geolocation for fields containing location data (i e , of type parsegeopoint parsegeopoint ) we have the following query options near //in order to select objects where one of their parsegeopoint type fields is near to another given geopoint, we use 1 guard let geopoint = try? parsegeopoint(latitude 37 38412167489413, longitude 122 01268034622319) else { 2 return 3 } 4 5 let query = profile query(near(key "lastloginlocation", geopoint geopoint)) 6 // near(key\ geopoint ) is a method provided by the parseswift sdk 7 8 let profiles = try? query find() // executes the query synchronously 9 10 query find { result in // executes the query asynchronously 11 // handle the result (of type result<\[profile], parseerror>) 12 } contains //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 struct myobject parseswift { 2 3 4 var apolygon parsepolygon? 5 6 7 } 8 9 guard let geopoint = try? parsegeopoint(latitude 37 38412167489413, longitude 122 01268034622319) else { 10 return 11 } 12 13 let query = myobject query(polygoncontains(key "apolygon", point geopoint)) 14 // polygoncontains(key\ point ) is a method provided by the parseswift sdk 15 16 let myobjects = try? query find() // executes the query synchronously 17 18 query find { result in // executes the query asynchronously 19 // handle the result (of type result<\[myobject], parseerror>) 20 } within geo box //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 guard let southwestpoint = try? parsegeopoint(latitude 37 38412167489413, longitude 122 01268034622319), 2 let northeastpoint = try? parsegeopoint(latitude 37 28412167489413, longitude 121 91268034622319) else { 3 return 4 } 5 6 let query = profile query(withingeobox(key "lastloginlocation", fromsouthwest southwestpoint, tonortheast northeastpoint)) 7 // withingeobox(key\ fromsouthwest\ tonortheast ) is a method provided by the parseswift sdk 8 9 let profiles = try? query find() // executes the query synchronously 10 11 query find { result in // executes the query asynchronously 12 // handle the result (of type result<\[profile], parseerror>) 13 } within km/mph //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 guard let geopoint = try? parsegeopoint(latitude 37 38412167489413, longitude 122 01268034622319) else { 2 return 3 } 4 5 let query1 = profile query(withinkilometers(key "lastloginlocation", geopoint geopoint, distance 100)) 6 // withinkilometers(key\ geopoint\ distance ) is a method provided by the parseswift sdk 7 8 let query2 = profile query(withinmiles(key "lastloginlocation", geopoint geopoint, distance 100)) 9 // withinmiles(key\ geopoint\ distance ) is a method provided by the parseswift sdk 10 11 // execute the corresponding query within polygon //used to select objects with a parsegeopoint type field in a given polygon the vertices of the polygon are represented by parsegeopoint objects 1 // create the vertices of the polygon (4 in this case) 2 guard let geopoint1 = try? parsegeopoint(latitude 37 48412167489413, longitude 122 11268034622319), 3 let geopoint2 = try? parsegeopoint(latitude 37 48412167489413, longitude 121 91268034622319), 4 let geopoint3 = try? parsegeopoint(latitude 37 38412167489413, longitude 121 91268034622319), 5 let geopoint4 = try? parsegeopoint(latitude 37 38412167489413, longitude 122 01268034622319) else { 6 return 7 } 8 9 let constraint queryconstraint = withinpolygon( 10 key "lastloginlocation", 11 points \[geopoint1, geopoint2, geopoint3, geopoint4] 12 ) 13 // withinpolygon(key\ points ) is a method provided by the parseswift sdk 14 15 let query = profile query(constraint) 16 17 let profiles = try? query find() // executes the query synchronously 18 19 query find { result in // executes the query asynchronously 20 // handle the result (of type result<\[profile], parseerror>) 21 } within radians //similar to ‘within kilometers’ or ‘within miles’ but the distance is expressed in radians 1 guard let geopoint = try? parsegeopoint(latitude 37 48412167489413, longitude 122 11268034622319) else { 2 return 3 } 4 5 let query = profile query(withinradians(key "lastloginlocation", geopoint geopoint, distance 1 5)) 6 // withinradians(key\ geopoint\ distance ) is a method provided by the parseswift sdk 7 8 let profiles = try? query find() // executes the query synchronously 9 10 query find { result in // executes the query asynchronously 11 // handle the result (of type result<\[profile], parseerror>) 12 } 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 parseswift sdk provides the following alternatives to accomplish that contained in //add a constraint to the query that requires a particular field to be contained in the provided array 1 let constraint queryconstraint = containedin(key "luckynumbers", array \[2, 7]) 2 // containedby(key\ array ) is a method provided by the parseswift sdk 3 4 let query = profile query(constraint) 5 6 let profiles = try? query find() // executes the query synchronously 7 8 query find { result in // executes the query asynchronously 9 // handle the result (of type result<\[profile], parseerror>) 10 } contained by //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 let constraint queryconstraint = containedby(key "luckynumbers", array \[2, 7]) 2 // containedby(key\ array ) is a method provided by the parseswift sdk 3 4 let query = profile query(constraint) 5 6 let profiles = try? query find() // executes the query synchronously 7 8 query find { result in // executes the query asynchronously 9 // handle the result (of type result<\[profile], parseerror>) 10 } contains all //add a constraint to the query that requires a particular array type field to contain every element of the provided array 1 let constraint queryconstraint = containsall(key "luckynumbers", array \[2, 7]) 2 // containsall(key\ array ) is a method provided by the parseswift sdk 3 4 let query = profile query(constraint) 5 6 let profiles = try? query find() // executes the query synchronously 7 8 query find { result in // executes the query asynchronously 9 // handle the result (of type result<\[profile], parseerror>) 10 } 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 parseswift sdk in this section we continue with the composition of queries and advanced queries exists //it is used to select objects depending on the value of a given field not being nil 1 let constraint queryconstraint = exists(key "membership") 2 // exists(key ) is a method provided by the parseswift sdk 3 4 let query = profile query(constraint) // will retrieve all profiles which have a membership 5 6 let profiles = try? query find() // executes the query synchronously 7 8 query find { result in // executes the query asynchronously 9 // handle the result (of type result<\[profile], parseerror>) 10 } does not exists //it is used to select objects depending on the value of a given field being nil 1 let constraint queryconstraint = doesnotexist(key "membership") 2 // doesnotexist(key ) is a method provided by the parseswift sdk 3 4 let query = profile query(constraint) // will retrieve all profiles which do not have a membership 5 6 let profiles = try? query find() // executes the query synchronously 7 8 query find { result in // executes the query asynchronously 9 // handle the result (of type result<\[profile], parseerror>) 10 } matches key in query //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 struct friend parseswift { 2 3 var numberofcontacts int? 4 5 } does not match key in query //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 let friendquery = friend query() // selects all friends objects 2 3 let constraint queryconstraint = doesnotmatchkeyinquery( 4 key "numberoffriends", 5 querykey "numberofcontacts", 6 query friendquery 7 ) 8 // doesnotmatchkeyinquery(key\ querykey\ query ) is a method provided by the parseswift sdk 9 10 let query = profile query(constraint) 11 12 let profiles = try? query find() // executes the query synchronously 13 14 query find { result in // executes the query asynchronously 15 // handle the result (of type result<\[profile], parseerror>) 16 } query ordering sorting the results from a query is key before starting to display or manipulate those results the parseswift sdk parseswift sdk provides the following options to accomplish that ascending //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 let query = profile query() order(\[ ascending("numberoffriends")]) 2 3 let profiles = try? query find() // executes the query synchronously 4 5 query find { result in // executes the query asynchronously 6 // handle the result (of type result<\[profile], parseerror>) 7 } descending //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 let query = profile query() order(\[ descending("numberoffriends")]) 2 3 let profiles = try? query find() // executes the query synchronously 4 5 query find { result in // executes the query asynchronously 6 // handle the result (of type result<\[profile], parseerror>) 7 } text score //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 extension profile parsequeryscorable { 2 var score double? { 3 // add the corresponding implementation 4 } 5 } 6 7 let query = profile query() sortbytextscore() 8 9 let profiles = try? query find() // executes the query synchronously 10 11 query find { result in // executes the query asynchronously 12 // handle the result (of type result<\[profile], parseerror>) 13 } 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 //calling the exclude( ) method on a query returns a new query that will exclude the fields passed (in variadic format) as the argument 1 let query = profile query() exclude("name", "numberoffriends") 2 3 let profiles = try? query find() // executes the query synchronously 4 5 query find { result in // executes the query asynchronously 6 // handle the result (of type result<\[profile], parseerror>) 7 } include //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 let query = profile query() include("membership") 2 3 let profiles = try? query find() // executes the query synchronously 4 5 query find { result in // executes the query asynchronously 6 // handle the result (of type result<\[profile], parseerror>) 7 } include all //when we need to retrieve the fields that conforms to the parseobject we call the includeall() method on the query 1 let query = profile query() includeall() 2 3 let profiles = try? query find() // executes the query synchronously 4 5 query find { result in // executes the query asynchronously 6 // handle the result (of type result<\[profile], parseerror>) 7 } select //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 let query = profile query() select("name", "birthday") 2 3 let profiles = try? query find() // executes the query synchronously 4 5 query find { result in // executes the query asynchronously 6 // handle the result (of type result<\[profile], parseerror>) 7 } pagination in large databases pagination, is a fundamental feature for querying and handling a large amount of results the parseswift sdk parseswift sdk provides the following methods to manage those situations limit //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 let query = profile query() limit(2) 2 3 let profiles = try? query find() // executes the query synchronously 4 5 query find { result in // executes the query asynchronously 6 // handle the result (of type result<\[profile], parseerror>) 7 } skip //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 let query = profile query() skip(2) 2 3 let profiles = try? query find() // executes the query synchronously 4 5 query find { result in // executes the query asynchronously 6 // handle the result (of type result<\[profile], parseerror>) 7 } with count //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 let query = profile query() 2 3 query withcount { result in // executes the query asynchronously 4 // handle the result (of type result<(\[profile], int), parseerror>) 5 } compound queries these methods will create compound queries, which can combine more than one query\<profile> query\<profile> instance to achieve more complex results and //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 let query1 = profile query("numberoffriends" > 10) 2 let query2 = profile query("numberoffriends" < 50) 3 4 let query = profile query(and(queries query1, query2)) 5 // and(queries ) is a method provided by the parseswift sdk it takes an array of queries and applies the and op on them 6 7 let profiles = try? query find() // executes the query synchronously 8 9 query find { result in // executes the query asynchronously 10 // handle the result (of type result<\[profile], parseerror>) 11 } nor //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 let query1 = profile query("numberoffriends" > 10) 2 let query2 = profile query("numberoffriends" < 50) 3 4 let query = profile query(nor(queries query1, query2)) 5 6 let profiles = try? query find() // executes the query synchronously 7 8 query find { result in // executes the query asynchronously 9 // handle the result (of type result<\[profile], parseerror>) 10 } or //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 let query1 = profile query("numberoffriends" > 10) 2 let query2 = profile query("numberoffriends" < 50) 3 4 let query = profile query(or(queries query1, query2)) 5 6 let profiles = try? query find() // executes the query synchronously 7 8 query find { result in // executes the query asynchronously 9 // handle the result (of type result<\[profile], parseerror>) 10 } database related these methods are related to the database preferences and operations aggregate //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 let query = profile query() 2 3 query aggregate(\[\["pipeline" 5]]) { result in 4 // handle the result (of type result<\[profile], parseerror>) 5 } explain //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 struct anycodable decodable { 2 // implement the corresponding properties 3} 4 5let query = profile query() 6 7let results = try? query findexplain() // executes the option synchronously 8 9// instantiate the completion block explicitely in order for the method findexplain(completion ) to infer the type of the returning results 10 let completion (result<\[anycodable], parseerror>) > void = { result in 11 // handle the result 12 } 13 14 // executes the option asynchronously 15 query findexplain(completion completion) readpreference //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 let query = profile query() readpreference("nearest") 2 3 let profiles = try? query find() // executes the query synchronously 4 5 query find { result in // executes the query asynchronously 6 // handle the result (of type result<\[profile], parseerror>) 7 } conclusion using the different methods, objects and operators provided by the parseswift sdk 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 https //github com/parse community/parse swift