iOS
...
Data Objects
ParseSwift SDK 쿼리 요리책: 고급 데이터 검색 기술
19 분
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를 사용하여 쿼리를 수행할 것입니다 시작하기 전에 객체와 데이터 유형에 대한 몇 가지 개념을 정립해야 합니다 이 가이드에서는 데이터베이스에 저장할 수 있는 모든 데이터 유형(구조체와 같은)을 객체라고 합니다 어떤 상황에서는 항목 또는 요소라고도 합니다 속성은 데이터 유형(주로 구조체)의 구성원입니다 그러나 여기에서는 일반적으로 필드라고 부릅니다 parseswift는 속성의 이름을 나타내기 위해 'key'라는 용어를 사용합니다 따라서 parseswift sdk parseswift sdk 의 모든 메서드에서 'key'로 레이블이 지정된 매개변수는 해당 값이 필드의 이름과 일치해야 함을 나타냅니다 전제 조건 이 튜토리얼을 완료하려면 다음이 필요합니다 back4app에서 생성된 앱 쿼리를 테스트할 기본 ios 앱 목표 다양한 방법을 탐색하기 위해서 query\<u> query\<u> 클래스가 쿼리를 실행하는 데 제공하는 query\<u> 클래스 이 가이드를 위해, 우리는 사람의 연락처에 대한 정보를 쿼리할 것입니다 이 데이터는 profile profile 클래스 아래에 저장됩니다 back4app 데이터베이스에서 ios 앱을 통해 프로필을 검색하기 위해, 우리는 다음과 같은 정보를 포함할 profile profile 구조체를 만들어야 합니다 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 } 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 데이터 객체 를 참조하세요 쿼리 검색기 이 메서드는 쿼리를 실행하고 결과를 검색하는 역할을 하며, 항상 쿼리 구현에 존재해야 합니다 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 } 고급 쿼리 지금까지 우리는 parseswift sdk parseswift sdk 를 사용하여 back4app 데이터베이스에서 객체를 선택하기 위해 적용할 수 있는 주요 조건을 소개했습니다 이 섹션에서는 쿼리의 구성과 고급 쿼리에 대해 계속 진행합니다 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 를 참조하십시오