iOS
...
Data Objects
Kuery Data Relasional dengan ParseSwift di iOS
13 mnt
kueri relasional pendahuluan dalam https //www back4app com/docs/ios/parse swift sdk/data objects/query cookbook kami menjelaskan bagaimana kami dapat melakukan berbagai kueri pada database back4app dalam panduan ini, kami fokus pada jenis kueri tertentu yang melibatkan objek dengan relasi prasyarat untuk menyelesaikan tutorial ini, anda akan membutuhkan sebuah aplikasi https //www back4app com/docs/get started/new parse app aplikasi ios dasar untuk menguji kueri tujuan kueri data relasional yang disimpan di database back4app menggunakan parseswift sdk parseswift sdk 1 tinjauan cepat tentang kelas query\<u> setiap kueri yang dilakukan pada database back4app dilakukan melalui kelas generik query\<u> query\<u> parameter generik u u (yang sesuai dengan protokol parseobject parseobject ) adalah tipe data dari objek yang ingin kita ambil dari database diberikan tipe data seperti myobject myobject , kita mengambil objek objek ini dari database back4app dengan cara berikut 1 import parseswift 2 3 struct myobject parseobject { 4 5 } 6 7 let query = myobject query() 8 9 // executes the query asynchronously 10 query find { result in 11 // handle the result (of type result<\[myobject], parseerror>) 12 } anda dapat membaca lebih lanjut tentang query\<u> query\<u> kelas https //github com/parse community/parse swift 2 simpan beberapa data di database back4app sebelum kita mulai mengeksekusi kueri, perlu untuk menyiapkan beberapa data di database back4app kita akan menyimpan lima jenis objek author 1 struct author parseobject { 2 3 var name string? 4 } book 1 struct book parseobject { 2 3 var title string? 4 var publisher publisher? 5 var publishingdate date? 6 } isbd 1 struct isbd parseobject { 2 3 var isbn string? 4 var book pointer\<book>? 5 } publisher 1 struct publisher parseobject { 2 3 var name string? 4 } bookstore 1 struct bookstore parseobject { 2 3 var name string? 4 } selain itu, untuk membangun kueri untuk data relasional, kita akan menerapkan hubungan berikut 1 1 hubungan antara buku buku dan isbd isbd 1\ n hubungan antara buku buku dan penerbit penerbit m\ n hubungan antara buku buku dan penulis penulis m\ n hubungan antara toko buku toko buku dan buku buku kami sekarang melanjutkan untuk menyimpan beberapa data di database back4app langkah ini dapat diimplementasikan menggunakan swift swift atau langsung dari konsol aplikasi anda di platform back4app swift //after setting up your xcode project, calling the following method should save some sample data on your back4app database 1 import parseswift 2 3 func savesampledata() { 4 do { 5 // authors 6 let aaronwriter = try author(name "aaron writer") save() 7 let beatricenovelist = try author(name "beatrice novelist") save() 8 let caseycolumnist = try author(name "casey columnist") save() 9 10 // publishers 11 let acaciapublishings = try publisher(name "acacia publishings") save() 12 let birchdistributions = try publisher(name "birch distributions") save() 13 14 // books with their corresponding isbd 15 let alovestorybook = try book( 16 title "a love story", 17 publisher acaciapublishings, 18 publishingdate date(string "05/07/1998") 19 ) save() 20 relation? add("authors", objects \[aaronwriter]) save() // establishes the m\ n relatin between book and author 21 22 // fetches the isbd associated to alovestorybook and establishes the 1 1 relation 23 if let book = alovestorybook, var isbd = try book fetch(includekeys \["isbd"]) isbd { 24 isbd book = try pointer\<book>(book) 25 = try isbd save() 26 } else { 27 fatalerror() 28 } 29 30 let benevolentelvesbook = try book( 31 title "benevolent elves", 32 publisher birchdistributions, 33 publishingdate date(string "11/30/2008") 34 ) save() 35 relation? add("authors", objects \[beatricenovelist]) save() // establishes the m\ n relatin between book and author 36 37 // fetches the isbd associated to benevolentelvesbook and establishes the 1 1 relation 38 if let book = benevolentelvesbook, var isbd = try book fetch(includekeys \["isbd"]) isbd { 39 isbd book = try pointer\<book>(book) 40 = try isbd save() 41 } else { 42 fatalerror() 43 } 44 45 let canyoubelieveitbook = try book( 46 title "can you believe it", 47 publisher birchdistributions, 48 publishingdate date(string "08/21/2018") 49 ) save() 50 relation? add("authors", objects \[aaronwriter, caseycolumnist]) save() // establishes the m\ n relatin between book and author 51 52 // fetches the isbd associated to canyoubelieveitbook and establishes the 1 1 relation 53 if let book = canyoubelieveitbook, var isbd = try book fetch(includekeys \["isbd"]) isbd { 54 isbd book = try pointer\<book>(book) 55 = try isbd save() 56 } else { 57 fatalerror() 58 } 59 60 // book store 61 guard let safealovestorybook = alovestorybook, 62 let safebenevolentelvesbook = benevolentelvesbook, 63 let safecanyoubelieveitbook = canyoubelieveitbook 64 else { 65 throw nserror( 66 domain bundle main description, 67 code 0, 68 userinfo \[nslocalizeddescriptionkey "failed to unwrapp stored books "] 69 ) 70 } 71 72 // saves the stores together with their 1\ n relation with book's 73 let booksoflove = try bookstore(name "books of love") save() 74 = try booksoflove relation? add("books", objects \[safealovestorybook]) save() 75 76 let fantasybooks = try bookstore(name "fantasy books") save() 77 = try fantasybooks relation? add("books", objects \[safebenevolentelvesbook]) save() 78 79 let generalbooks = try bookstore(name "general books") save() 80 = try generalbooks relation? add("books", objects \[safealovestorybook, safecanyoubelieveitbook]) save() 81 82 } catch let error as parseerror { 83 print("error \n", error message) 84 } catch { 85 print("error \n", error localizeddescription) 86 } 87 } back4app's console //a quick way to insert elements on your back4app database is via the console located in your app’s api section once you are there, you can start running javascript code to save the sample data 1 // authors 2 const aaronwriter = new parse object('author'); 3 aaronwriter set('name', 'aaron writer'); 4 await aaronwriter save(); 5 6 const beatricenovelist = new parse object('author'); 7 beatricenovelist set('name', 'beatrice novelist'); 8 await beatricenovelist save(); 9 10 const caseycolumnist = new parse object('author'); 11 caseycolumnist set('name', 'casey columnist'); 12 await caseycolumnist save(); 13 14 // publishers 15 const acaciapublishings = new parse object('publisher'); 16 acaciapublishings set('name', 'acacia publishings'); 17 await acaciapublishings save(); 18 19 const birchdistributions = new parse object('publisher'); 20 birchdistributions set('name', 'birch distributions'); 21 await birchdistributions save(); 22 23 // books with their corresponding isbd 24 const alovestoryisbd = new parse object('isbd'); 25 alovestoryisbd set('isbn', '9781401211868'); 26 await alovestoryisbd save(); 27 28 const alovestorybook = new parse object('book'); 29 alovestorybook set('title', 'a love story'); 30 alovestorybook set('publisher', acaciapublishings); 31 alovestorybook set('publishingdate', new date('05/07/1998')); 32 alovestorybook set('isbd', alovestoryisbd); 33 const bookarelation = alovestorybook relation("authors"); 34 bookarelation add(aaronwriter); 35 await alovestorybook save(); 36 alovestoryisbd set('book', alovestorybook topointer()); 37 await alovestoryisbd save(); 38 39 const benevolentelvesisbd = new parse object('isbd'); 40 benevolentelvesisbd set('isbn', '9781401211868'); 41 await benevolentelvesisbd save(); 42 43 const benevolentelvesbook = new parse object('book'); 44 benevolentelvesbook set('title', 'benevolent elves'); 45 benevolentelvesbook set('publisher', birchdistributions); 46 benevolentelvesbook set('publishingdate', new date('11/31/2008')); 47 benevolentelvesbook set('isbd', benevolentelvesisbd); 48 const bookbrelation = benevolentelvesbook relation("authors"); 49 bookbrelation add(beatricenovelist); 50 await benevolentelvesbook save(); 51 benevolentelvesisbd set('book', benevolentelvesbook topointer()); 52 await benevolentelvesisbd save(); 53 54 const canyoubelieveitisbd = new parse object('isbd'); 55 canyoubelieveitisbd set('isbn', '9781401211868'); 56 await canyoubelieveitisbd save(); 57 58 const canyoubelieveitbook = new parse object('book'); 59 canyoubelieveitbook set('title', 'can you believe it?'); 60 canyoubelieveitbook set('publisher', birchdistributions); 61 canyoubelieveitbook set('publishingdate', new date('08/21/2018')); 62 canyoubelieveitbook set('isbd', canyoubelieveitisbd); 63 const bookcrelation = canyoubelieveitbook relation("authors"); 64 bookcrelation add(aaronwriter); 65 bookcrelation add(caseycolumnist); 66 await canyoubelieveitbook save(); 67 canyoubelieveitisbd set('book', canyoubelieveitbook topointer()); 68 await canyoubelieveitisbd save(); 69 70 // book store 71 const booksoflovestore = new parse object('bookstore'); 72 booksoflovestore set('name', 'books of love'); 73 const bookstorearelation = booksoflovestore relation("books"); 74 bookstorearelation add(alovestorybook); 75 await booksoflovestore save(); 76 77 const fantasybooksstore = new parse object('bookstore'); 78 fantasybooksstore set('name', 'fantasy books'); 79 const bookstorebrelation = fantasybooksstore relation("books"); 80 bookstorebrelation add(benevolentelvesbook); 81 await fantasybooksstore save(); 82 83 const generalbooksstore = new parse object('bookstore'); 84 generalbooksstore set('name', 'general books'); 85 const bookstorecrelation = generalbooksstore relation("books"); 86 bookstorecrelation add(alovestorybook); 87 bookstorecrelation add(canyoubelieveitbook); 88 await generalbooksstore save(); 3 kuery data setelah basis data memiliki beberapa data contoh untuk dikerjakan, kita mulai mengeksekusi berbagai jenis kueri yang terkait dengan relasi yang dijelaskan sebelumnya kueri yang melibatkan relasi 1 1 diberikan dua tipe data yang berbagi relasi 1 1 ( buku buku dan isbd isbd dalam hal ini), kita dapat mengambil satu dari yang lain sebagai berikut cara kita mengimplementasikan relasi di buku buku memungkinkan kita untuk mengambil isbd isbd terkait hanya dengan memanggil metode include( ) include( ) pada kueri mari kita ambil isbd isbd dari buku a love story 1 let alovestorybookquery = book query("title" == "a love story") include("isbd") // note how we include the isbd with the include( ) method 2 3 let book = try? alovestorybookquery first() // retrieves synchronously the book including its isbd 4 5 alovestorybookquery first { result in // retrieves asynchronously the book including its isbd 6 // handle the result (of type result\<book, parseerror>) 7 } di sisi lain, kueri untuk mengambil objek buku buku yang terkait dengan isbd isbd tertentu diimplementasikan dengan cara berikut dengan melihat implementasi isbd isbd , kita mencatat bahwa relasi diwakili oleh properti buku buku (dari tipe pointer\<book> pointer\<book> ) pointer ini menyediakan serangkaian metode dan properti untuk mengambil informasi tentang objek yang ditunjuknya secara khusus, kita memanggil metode fetch( ) fetch( ) pada properti buku buku untuk mengambil buku buku 1 let someisbd isbd 2 3 let book book? = try? someisbd book? fetch() // retrieves synchronously the book asscociated to someisbd 4 5 someisbd book? fetch { result in // retrieves asynchronously the book asscociated to someisbd 6 // handle the result (of type result\<book, parseerror>) 7 } kita harus mencatat bahwa implementasi untuk 1 1 hubungan ini tidak unik tergantung pada kasus penggunaan anda, anda dapat mengimplementasikan 1 1 hubungan dengan cara yang berbeda kueri yang melibatkan hubungan 1\ n dalam skenario di mana kita perlu mengkueri semua buku yang diterbitkan oleh penerbit tertentu, kita pertama tama perlu mengambil penerbit tersebut misalnya, kita pertama tama mengambil objek data yang terkait dengan penerbit acacia publishings tergantung pada situasi, proses ini dapat bervariasi 1 do { 2 // using the object's objectid 3 let acaciapublishings = try publisher(objectid "some object id") fetch() 4 5 // or 6 // using a query 7 let acaciapublishings = try publisher query("name" == "acacia publishings") first() // returns (synchronously) the first publisher with name 'acacia publishings' the constraint is constructed using the == operator provided by the parseswift sdk 8 9 // to be completed below 10 } catch { 11 // hanlde the error (of type parseerror) 12 } sekarang kita memiliki akses ke acaciapublishings acaciapublishings , kita dapat membangun kueri untuk mengambil buku buku terkaitnya kita melanjutkan untuk membuat kueri dengan menginstansiasi kelas query\<book> query\<book> dalam hal ini, kelas ini diinstansiasi menggunakan metode statis query( ) query( ) yang disediakan oleh objek book book argumen (variadic) untuk metode ini adalah objek queryconstraint queryconstraint standar oleh karena itu, buku buku yang kita cari diambil dengan cuplikan berikut 1 do { 2 let acaciapublishings = try publisher query("name" == "acacia publishings") first() // returns the first publisher with name 'acacia publishings' 3 4 let constraint queryconstraint = try "publisher" == publisher 5 let query = book query(constraint) // creates the query to retrieve all book objects where its publisher field equalt to 'acaciapublishings' 6 7 let books \[book] = try query find() // executes the query synchronously 8 9 // books should contain only one element the book 'a love story' 10 } catch { 11 // hanlde the error (of type parseerror) 12 } implementasi asinkron untuk cuplikan di atas dapat ditulis dengan cara berikut 1 // we retrieve the publisher with name 'acacia publishings' 2 publisher query("name" == "acacia publishings") first { result in 3 switch result { 4 case success(let publisher) 5 guard let constraint queryconstraint = try? "publisher" == publisher else { fatalerror() } 6 7 // then, we retrieve the books with the corresponding constraint 8 book query(constraint) find { result in 9 switch result { 10 case success(let books) 11 // books should contain only one element the book 'a love story' 12 break 13 case failure(let error) 14 // handle the error (of type parseerror) 15 break 16 } 17 } 18 19 case failure(let error) 20 // handle the error (of type parseerror) 21 break 22 } 23 } query yang melibatkan hubungan m\ n (kasus 1) untuk menggambarkan kasus ini, kita mempertimbangkan skenario berikut; kita ingin mencantumkan semua toko yang memiliki buku yang diterbitkan setelah tanggal tertentu (misalnya, 01/01/2010 ) pertama, kita memerlukan query perantara untuk memilih buku buku tersebut selanjutnya, kita membangun query utama untuk mencantumkan toko toko oleh karena itu, kita menyiapkan query pertama untuk buku buku 1 let booksquery = book query("publishingdate" > date(string "01/01/2010")) // we construct the date constraint using the > operator provided by the parseswift sdk 2 3 do { 4 let books = try booksquery find() 5 // to be completed below 6 } catch let error as parseerror { 7 // handle any potential error 8 } catch { 9 // handle any potential error 10 } kami kemudian membangun kueri toko menggunakan booksquery booksquery ’s results metode containedin( array ) containedin( array ) mengembalikan batasan yang kami butuhkan untuk kasus ini 1 let booksquery = book query("publishingdate" > date(string "01/01/2010")) // we construct the date constraint using the > operator provided by the parseswift sdk 2 3 do { 4 let books = try booksquery find() 5 6 // here is where we construct the stores' query with the corresponding constraint 7 let storesquery = bookstore query(try containedin(key "books", array books)) 8 9 let stores = try storesquery find() 10 11 // stores should containt only one element the 'general books' bookstore 12 } catch let error as parseerror { 13 // handle any potential error 14 } catch { 15 // handle any potential error 16 } demikian pula, kami dapat menerapkan proses ini secara asinkron 1 let booksquery = book query("publishingdate" > date(string "01/01/2010")) // we construct the date constraint using the > operator provided by the parseswift sdk 2 3 booksquery find { result in 4 switch result { 5 case success(let books) 6 guard let constraint = try? containedin(key "books", array books) else { fatalerror() } 7 let storesquery = bookstore query(constraint) 8 9 storesquery find { result in 10 switch result { 11 case success(let stores) 12 13 case failure(let error) 14 // handle the error (of type parseerror) 15 } 16 } 17 case failure(let error) 18 // handle the error (of type parseerror) 19 } 20 } kueri yang melibatkan hubungan m\ n (kasus 2) misalkan kita perlu memilih semua toko yang memiliki buku yang ditulis oleh seorang penulis tertentu, katakanlah, aaron writer untuk mencapai ini, kita memerlukan dua kueri tambahan sebuah kueri ( query\<author> query\<author> ) untuk mendapatkan objek yang terkait dengan penulis aaron writer sebuah kueri ( query\<book> query\<book> ) untuk memilih semua buku yang ditulis oleh aaron writer kueri utama ( query\<bookstore> query\<bookstore> ) untuk memilih toko yang kita cari prosedur untuk menerapkan kueri ini sangat mirip dengan yang sebelumnya 1 let authorquery = author query("name" == "aaron writer") // the first query to retrieve the data object associated to 'aaron writer' 2 3 do { 4 let aaronwriter = try authorquery first() 5 6 let booksquery = book query(try containedin(key "authors", array \[aaronwriter])) // the second query to retrieve the books written by 'aaron writer' 7 8 let books = try booksquery find() 9 10 let storesquery = bookstore query(try containedin(key "books", array books)) // the main query to select the stores where the author ('aaron writer') has his books available 11 12 let stores = try storesquery find() 13 14 // stores should contain two items 'books of love' and 'general books' 15 } catch let error as parseerror { 16 // handle the error 17 } catch { 18 // handle the error 19 } kesimpulan dengan parseswift sdk parseswift sdk , kami dapat membangun kueri relasional yang memungkinkan kami untuk memilih item berdasarkan jenis hubungan yang mereka miliki dengan tipe data lainnya