iOS
...
Data Objects
Effiziente Abfragen mit dem ParseSwift SDK für iOS
20 min
abfrage kochbuch für parseswift einführung in dem grundlegenden abfragen https //www back4app com/docs/ios/parse swift sdk/data objects/basic queries leitfaden haben wir einige der grundlegenden methoden vorgestellt, um abfragen zu erstellen und daten aus einer back4app datenbank abzurufen das parseswift sdk parseswift sdk bietet praktische möglichkeiten, um komplexere abfragen für reale anwendungen zu erstellen in diesem leitfaden werden sie tief in die query\<u> query\<u> (generische) klasse eintauchen und alle methoden sehen, die sie verwenden können, um ihre abfragen zu erstellen sie werden eine einfache datenbankklasse (genannt profil profil ) mit einigen simulierten daten verwenden, um abfragen mit swift durchzuführen bevor wir beginnen, sollten wir einige konzepte über objekte und datentypen festlegen in diesem leitfaden beziehen wir uns auf objekte als jeden datentyp (wie strukturen), der in einer datenbank gespeichert werden kann in einigen situationen werden sie auch als elemente oder gegenstände bezeichnet eigenschaften sind die mitglieder eines datentyps (hauptsächlich strukturen) in diesem zusammenhang nennen wir sie normalerweise felder parseswift verwendet den begriff ‚schlüssel‘, um den namen der eigenschaften anzuzeigen daher muss jeder parameter, der in einer methode des parseswift sdk parseswift sdk als ‚schlüssel‘ gekennzeichnet ist, darauf hinweisen, dass sein wert mit dem namen eines feldes übereinstimmen muss voraussetzungen um dieses tutorial abzuschließen, benötigen sie eine app die auf back4app erstellt wurde eine grundlegende ios app, um abfragen zu testen ziel um die verschiedenen methoden zu erkunden, die die query\<u> query\<u> klasse bereitstellt, um abfragen auszuführen die query\<u> klasse für diesen leitfaden werden wir informationen über die kontakte einer person abfragen diese daten werden unter der klasse profile profile in der back4app datenbank gespeichert um profile über eine ios app abzurufen, müssen wir die profile profile struktur erstellen, die solche informationen enthalten wird 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 } zusätzlich verwenden wir das objekt mitgliedschaft mitgliedschaft um zu zeigen, wie abfragen zwischen profil profil und mitgliedschaft mitgliedschaft miteinander interagieren die mitgliedschaft mitgliedschaft struktur wird folgendermaßen implementiert 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 } jede abfrageoperation auf einer back4app datenbank wird durch die generische klasse query\<profil> query\<profil> durchgeführt, wobei der generische typ profil profil (der dem parseswift parseswift protokoll entspricht) das objekt ist, das wir abrufen möchten nachdem wir ein query\<profil> query\<profil> objekt instanziiert haben, müssen wir eine seiner find( ) find( ) methoden aufrufen (siehe den leitfaden für grundlegende abfragen https //www back4app com/docs/ios/parse swift sdk/data objects/basic queries ) um das ergebnis zu verarbeiten, das von der abfrage zurückgegeben wird sie können mehr über die query\<u> query\<u> klasse hier in der offiziellen dokumentation https //github com/parse community/parse swift datenobjekte speichern mit dem folgenden snippet erstellen und speichern wir einige daten, um verschiedene abfragen zu testen 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 } sobald der obige code ausgeführt wird, sollten wir eine kleine datenbank haben, mit der wir arbeiten können erfahren sie mehr darüber, wie sie daten mit parseswift speichern können unter ios datenobjekte abfrage retriever diese methoden sind dafür verantwortlich, die abfrage auszuführen und ihre ergebnisse abzurufen, und sind immer in ihrer abfrageimplementierung vorhanden 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 } abfragen mit einschränkungen für felder vom typ string wir beginnen nun, spezifische einschränkungen auf abfragen anzuwenden zunächst werden wir einschränkungen auferlegen, die objekte nur auswählen, wenn ein string string feld eine gegebene bedingung erfüllt diese und jede andere art von einschränkung, die auf ein query\<profile> query\<profile> objekt angewendet wird, erfolgt über das queryconstraint queryconstraint objekt im folgenden und in den nächsten abschnitten erläutern wir, wie man solche einschränkungen erstellt 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 } abfragen mit einschränkungen auf vergleichbaren feldern es ist sehr häufig, objekte auszuwählen, bei denen ein bestimmtes zahlenfeld gleich, unterschiedlich, größer oder kleiner als ein gegebener wert sein muss dies wird durch die verwendung der folgenden vergleichbaren operationen erreicht 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 } diese einschränkungen gelten auch für datum datum typfelder zum beispiel können wir haben 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 } für bool bool typfelder haben wir die gleich und ungleich optionen zum beispiel 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 } abfragen mit einschränkungen, die geolokalisierung betreffen für felder, die standortdaten enthalten (d h vom typ parsegeopoint parsegeopoint ) haben wir die folgenden abfrageoptionen 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 } abfragen mit einschränkungen, die array typfelder betreffen wenn die objekte, die wir abrufen möchten, eine bestimmte bedingung für eines ihrer array felder erfüllen müssen, parseswift sdk parseswift sdk bietet die folgenden alternativen, um dies zu erreichen 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 } erweiterte abfragen bis jetzt haben wir die hauptbedingungen eingeführt, die wir auf ein bestimmtes feld anwenden können, um objekte aus einer back4app datenbank auszuwählen, indem wir parseswift sdk parseswift sdk in diesem abschnitt setzen wir die zusammensetzung von abfragen und erweiterten abfragen fort 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 } abfrageordnung die sortierung der ergebnisse aus einer abfrage ist entscheidend, bevor man beginnt, diese ergebnisse anzuzeigen oder zu manipulieren die parseswift sdk parseswift sdk bietet die folgenden optionen, um dies zu erreichen 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 } feldauswahl je nach den während einer abfrage benötigten daten kann dies mehr oder weniger zeit in anspruch nehmen in einigen szenarien kann es ausreichen, bestimmte felder aus einem objekt abzurufen und die unnötigen felder zu ignorieren darüber hinaus vermeiden wir durch die auswahl nur der felder, die wir aus einer abfrage benötigen, das überladen und verbessern die leistung des abrufprozesses 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 } seitenumbruch in großen datenbanken ist die seitenumbruch eine grundlegende funktion zum abfragen und verarbeiten einer großen anzahl von ergebnissen die parseswift sdk parseswift sdk bietet die folgenden methoden zur verwaltung dieser situationen 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 } zusammengesetzte abfragen diese methoden erstellen zusammengesetzte abfragen, die mehr als eine abfrage\<profil> abfrage\<profil> instanz kombinieren, um komplexere ergebnisse zu erzielen 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 } datenbankbezogen diese methoden beziehen sich auf die datenbankeinstellungen und operationen 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 } fazit durch die verwendung der verschiedenen methoden, objekte und operatoren, die vom parseswift sdk parseswift sdk , konnten wir verstehen, wie man objekte aus einer back4app datenbank konstruiert und abruft mit diesem kochbuch sollten sie in der lage sein, abfragen mit sehr flexiblen einschränkungen durchzuführen und die ergebnisse gemäß ihrem anwendungsfall zu verwalten für weitere details zu einem der oben genannten themen können sie auf das parseswift repository https //github com/parse community/parse swift verweisen