iOS
...
Data Objects
เรียนรู้และใช้งานการสอบถามใน ParseSwift SDK อย่างมีประสิทธิภาพ
18 นาที
คู่มือการสอบถามสำหรับ parseswift บทนำ ใน การสอบถามพื้นฐาน https //www back4app com/docs/ios/parse swift sdk/data objects/basic queries คู่มือนี้ เราได้แนะนำวิธีการพื้นฐานบางประการในการสร้างการสอบถามและดึงข้อมูลจาก back4app ฐานข้อมูล。 parseswift sdk parseswift sdk มีวิธีการที่ใช้งานได้จริงในการสร้างการสอบถามที่ซับซ้อนมากขึ้นสำหรับแอปพลิเคชันในโลกจริง ในคู่มือนี้ คุณจะได้ดำดิ่งลึกลงไปใน query\<u> query\<u> (คลาสทั่วไป) และดูวิธีการทั้งหมดที่คุณสามารถใช้ในการสร้างการสอบถามของคุณ คุณจะใช้คลาสฐานข้อมูลที่เรียบง่าย (ชื่อว่า profile profile ) พร้อมข้อมูลที่จำลองขึ้นเพื่อทำการสอบถามโดยใช้ swift ก่อนที่จะเริ่มต้น เราควรกำหนดแนวคิดบางประการเกี่ยวกับวัตถุและประเภทข้อมูล ในคู่มือนี้ เราเรียกว่าวัตถุว่าเป็นประเภทข้อมูลใด ๆ (เช่น structs) ที่สามารถเก็บไว้ในฐานข้อมูล ในบางสถานการณ์ พวกเขายังถูกเรียกว่าเป็นรายการหรือองค์ประกอบ คุณสมบัติเป็นสมาชิกของประเภทข้อมูล (ส่วนใหญ่เป็น structs) อย่างไรก็ตาม ที่นี่เรามักจะเรียกพวกเขาว่าฟิลด์ parseswift ใช้คำว่า ‘key’ เพื่อระบุชื่อคุณสมบัติ ดังนั้น พารามิเตอร์ใด ๆ ที่มีป้ายกำกับว่า ‘key’ ในวิธีการใด ๆ จาก parseswift sdk parseswift sdk หมายความว่าค่าของมันต้องตรงกับชื่อฟิลด์ ข้อกำหนดเบื้องต้น ในการทำตามบทเรียนนี้ คุณจะต้องการ แอป ที่สร้างขึ้นบน back4app แอป ios พื้นฐานเพื่อทดสอบการสอบถาม เป้าหมาย เพื่อสำรวจวิธีการต่างๆ ที่ query\<u> query\<u> คลาสให้เพื่อดำเนินการค้นหา คลาส query\<u> สำหรับคู่มือนี้ เราจะค้นหาข้อมูลเกี่ยวกับการติดต่อของบุคคล ข้อมูลเหล่านี้ถูกเก็บไว้ภายใต้คลาส profile profile บน back4app ฐานข้อมูล เพื่อดึงโปรไฟล์ผ่านแอป ios เราจำเป็นต้องสร้าง profile profile struct ที่จะเก็บข้อมูลดังกล่าว 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 } นอกจากนี้เรายังใช้วัตถุ membership membership เพื่อแสดงให้เห็นว่าคำค้นระหว่าง profile profile และ membership membership มีปฏิสัมพันธ์กันอย่างไร membership membership โครงสร้างถูกนำไปใช้ในลักษณะดังต่อไปนี้ 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 } การดำเนินการค้นหาใด ๆ บนฐานข้อมูล back4app จะดำเนินการโดยคลาสทั่วไป query\<profile> query\<profile> ซึ่งประเภททั่วไป profile profile (ซึ่งสอดคล้องกับ parseswift parseswift โปรโตคอล) คือวัตถุที่เราพยายามดึงข้อมูล หลังจากสร้างวัตถุ query\<profile> query\<profile> เราต้องเรียกใช้หนึ่งใน find( ) find( ) วิธีการของมัน (ดูที่ คู่มือการค้นหาพื้นฐาน https //www back4app com/docs/ios/parse swift sdk/data objects/basic queries ) เพื่อจัดการกับผลลัพธ์ที่ส่งกลับจากการค้นหา คุณสามารถอ่านเพิ่มเติมเกี่ยวกับ query\<u> query\<u> คลาส ที่นี่ในเอกสารทางการ https //github com/parse community/parse swift การบันทึกวัตถุข้อมูล ด้วยโค้ดส่วนนี้เราสร้างและบันทึกข้อมูลบางอย่างเพื่อลองสอบถามต่างๆ 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 } เมื่อรหัสข้างต้นถูกดำเนินการ เราควรมีฐานข้อมูลขนาดเล็กให้ทำงานด้วย ดูเพิ่มเติมเกี่ยวกับวิธีการบันทึกข้อมูลด้วย parseswift ที่ ios data objects ตัวดึงข้อมูลการค้นหา วิธีการเหล่านี้มีหน้าที่ในการดำเนินการค้นหาและดึงผลลัพธ์ของมัน ซึ่งจะต้องมีอยู่เสมอในการนำไปใช้การค้นหาของคุณ 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 } การค้นหาที่มีข้อจำกัดในฟิลด์ประเภทสตริง เราจะเริ่มใช้ข้อจำกัดเฉพาะกับการค้นหา โดยเริ่มจากการกำหนดข้อจำกัดที่เลือกวัตถุเฉพาะเมื่อ string string ฟิลด์ประเภทนี้ตรงตามเงื่อนไขที่กำหนด ข้อจำกัดนี้และข้อจำกัดประเภทอื่น ๆ ที่กำหนดกับ query\<profile> query\<profile> วัตถุจะทำผ่าน queryconstraint queryconstraint วัตถุ ด้านล่างและในส่วนถัดไปเราจะอธิบายวิธีการสร้างข้อจำกัดดังกล่าว 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 } คำถามที่มีข้อจำกัดเกี่ยวกับฟิลด์ที่เปรียบเทียบได้ เป็นเรื่องปกติมากที่จะพยายามเลือกวัตถุที่ฟิลด์ประเภทจำนวนหนึ่งต้องเท่ากัน แตกต่าง มากกว่า หรือน้อยกว่าค่าที่กำหนด สิ่งนี้ทำได้โดยการใช้การดำเนินการเปรียบเทียบต่อไปนี้ 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 } ข้อจำกัดเหล่านี้ยังใช้กับ วันที่ วันที่ ประเภทฟิลด์ได้ ตัวอย่างเช่น เราสามารถมี 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 } สำหรับ bool bool ประเภทฟิลด์ เรามี เท่ากับ และ ไม่เท่ากับ ตัวเลือก ตัวอย่างเช่น 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 } คำถามที่มีข้อจำกัดเกี่ยวกับข้อมูลตำแหน่ง สำหรับฟิลด์ที่มีข้อมูลตำแหน่ง (เช่น ประเภท parsegeopoint parsegeopoint ) เรามีตัวเลือกการค้นหาดังต่อไปนี้ 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 } คำถามที่มีข้อจำกัดเกี่ยวกับฟิลด์ประเภทอาเรย์ เมื่อวัตถุที่เราต้องการดึงข้อมูลต้องเป็นไปตามเงื่อนไขที่กำหนดในฟิลด์ประเภทอาร์เรย์ใด ๆ parseswift sdk parseswift sdk มีทางเลือกต่อไปนี้เพื่อทำให้สำเร็จ 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 } การค้นหาขั้นสูง จนถึงตอนนี้เราได้แนะนำเงื่อนไขหลักที่เราสามารถใช้ในการเลือกวัตถุจากฐานข้อมูล back4app โดยใช้ parseswift sdk parseswift sdk ในส่วนนี้เราจะดำเนินการต่อด้วยการประกอบคำถามและการค้นหาขั้นสูง 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 } การจัดเรียงคำค้น การจัดเรียงผลลัพธ์จากคำค้นเป็นกุญแจสำคัญก่อนที่จะเริ่มแสดงหรือจัดการกับผลลัพธ์เหล่านั้น parseswift sdk parseswift sdk มีตัวเลือกต่อไปนี้เพื่อทำให้สำเร็จ 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 } การเลือกฟิลด์ ขึ้นอยู่กับข้อมูลที่ต้องการในระหว่างการค้นหา สิ่งนี้อาจใช้เวลามากหรือน้อย ในบางสถานการณ์อาจเพียงพอที่จะดึงข้อมูลบางฟิลด์จากวัตถุและละเว้นฟิลด์ที่ไม่จำเป็น นอกจากนี้ โดยการเลือกเฉพาะฟิลด์ที่เราต้องการจากการค้นหา เราหลีกเลี่ยงการดึงข้อมูลมากเกินไปและปรับปรุงประสิทธิภาพในกระบวนการดึงข้อมูล 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 } การแบ่งหน้า ในฐานข้อมูลขนาดใหญ่ การแบ่งหน้าเป็นฟีเจอร์พื้นฐานสำหรับการสอบถามและจัดการผลลัพธ์จำนวนมาก parseswift sdk parseswift sdk มีวิธีการต่อไปนี้เพื่อจัดการกับสถานการณ์เหล่านั้น 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 } การสอบถามแบบผสม วิธีการเหล่านี้จะสร้างคำถามเชิงซ้อน ซึ่งสามารถรวมมากกว่าหนึ่ง query\<profile> query\<profile> อินสแตนซ์เพื่อให้ได้ผลลัพธ์ที่ซับซ้อนมากขึ้น 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 } เกี่ยวกับฐานข้อมูล วิธีการเหล่านี้เกี่ยวข้องกับการตั้งค่าและการดำเนินการของฐานข้อมูล 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 } บทสรุป โดยใช้วิธีการต่างๆ วัตถุและตัวดำเนินการที่จัดเตรียมโดย parseswift sdk parseswift sdk , เราสามารถเข้าใจวิธีการสร้างและดึงวัตถุจาก back4app ฐานข้อมูล ด้วยตำราอาหารนี้คุณควรจะสามารถดำเนินการสอบถามด้วยข้อจำกัดที่ยืดหยุ่นมากและจัดการผลลัพธ์ตามกรณีการใช้งานของคุณ สำหรับรายละเอียดเพิ่มเติมเกี่ยวกับหัวข้อใด ๆ ข้างต้น คุณสามารถดูที่ ที่เก็บ parseswift https //github com/parse community/parse swift