iOS
...
Data Objects
การจัดการความสัมพันธ์ใน ParseSwift SDK สำหรับ iOS
15 นาที
ความสัมพันธ์ บทนำ ความสัมพันธ์เป็นคุณลักษณะพื้นฐานในการจัดระเบียบวัตถุข้อมูลที่เก็บไว้ในฐานข้อมูล parseswift มีเครื่องมือและวิธีการที่จำเป็นในการสร้างความสัมพันธ์ระหว่างคลาสในฐานข้อมูล back4app ของคุณ ขึ้นอยู่กับกรณีการใช้งาน เราสามารถระบุประเภทความสัมพันธ์ดังต่อไปนี้ 1 1 ความสัมพันธ์ที่เชื่อมโยงวัตถุข้อมูลเพียงสองวัตถุเท่านั้น 1\ n ความสัมพันธ์ระหว่างวัตถุข้อมูลหนึ่งและ n n วัตถุข้อมูล n\ n ความสัมพันธ์ระหว่าง n n วัตถุข้อมูลกับ n n วัตถุข้อมูล ตามที่เราจะเห็นด้านล่าง การดำเนินการ 1 1 ความสัมพันธ์นั้นค่อนข้างตรงไปตรงมา สำหรับ 1\ n และ n\ n ความสัมพันธ์ การดำเนินการเกี่ยวข้องกับ parserelation parserelation วัตถุที่จัดเตรียมโดย parseswift sdk มีทางเลือกเพิ่มเติมสำหรับการดำเนินการ 1\ n และ n\ n ความสัมพันธ์ อย่างไรก็ตาม เนื่องจากประสิทธิภาพและประสิทธิผล เราขอแนะนำให้ปฏิบัติตามวิธีการของเรา บทเรียนนี้ใช้แอปพื้นฐานที่สร้างขึ้นใน xcode 12 และ ios 14 ในทุกเวลา คุณสามารถเข้าถึงโครงการทั้งหมดได้ผ่านที่เก็บ github ของเรา ที่เก็บตัวอย่าง ios เป้าหมาย เพื่อเข้าใจว่าความสัมพันธ์ถูกนำไปใช้ในฐานข้อมูล back4app อย่างไร ข้อกำหนดเบื้องต้น ในการทำให้การเริ่มต้นนี้เสร็จสมบูรณ์ คุณต้องการ xcode แอปที่สร้างขึ้นที่ back4app ติดตาม บทแนะนำการสร้างแอป parse ใหม่ เพื่อเรียนรู้วิธีการสร้างแอป parse ที่ back4app หมายเหตุ ติดตาม บทแนะนำการติดตั้ง parse sdk (swift) เพื่อสร้างโปรเจกต์ xcode ที่เชื่อมต่อกับ back4app การเข้าใจแอปหนังสือของเรา เทมเพลตโปรเจกต์คือแอปหนังสือที่ผู้ใช้กรอกรายละเอียดหนังสือเพื่อบันทึกลงในฐานข้อมูล back4app บนหน้าจอหลักของแอป คุณจะพบแบบฟอร์มเพื่อทำเช่นนั้น โดยใช้ + + ปุ่มที่ตั้งอยู่ด้านขวาบนของแถบนำทาง เราสามารถเพิ่ม ผู้จัดพิมพ์ , ประเภท และ ผู้เขียน ได้ตามต้องการ เมื่อผู้ใช้กรอกหนังสือแล้ว พวกเขาสามารถใช้ เพิ่มหนังสือ เพิ่มหนังสือ ปุ่มเพื่อบันทึกหนังสือลงในฐานข้อมูล back4app ของพวกเขา นอกจากนี้ ปุ่ม แสดงหนังสือ แสดงหนังสือ จะช่วยให้เราสามารถแสดงหนังสือทั้งหมดที่ผู้ใช้เพิ่มและดูความสัมพันธ์ของพวกเขากับผู้จัดพิมพ์และผู้เขียนได้ การอ้างอิงอย่างรวดเร็วของคำสั่งที่เราจะใช้ เราใช้วัตถุ ผู้เขียน ผู้เขียน , ผู้จัดพิมพ์ ผู้จัดพิมพ์ , isbn isbn และ หนังสือ หนังสือ genre 1 import foundation 2 import parseswift 3 4 struct genre parseobject { 5 6 7 var name string? 8 9 10 } author 1 import foundation 2 import parseswift 3 4 struct author parseobject { 5 6 7 var name string? 8 9 10 } publisher 1 import foundation 2 import parseswift 3 4 struct publisher parseobject { 5 6 7 var name string? 8 9 10 } isbn 1 import foundation 2 import parseswift 3 4 struct isbn parseobject { 5 6 7 var name string? 8 9 10 } book 1 import foundation 2 import parseswift 3 4 struct book parseobject { 5 6 7 var title string? 8 var publishingyear int? 9 var genre genre? 10 var isbn isbn? 11 12 13 } ก่อนที่จะเก็บตัวอย่างของวัตถุเหล่านี้ในฐานข้อมูล back4app คุณสมบัติทั้งหมดของพวกเขาต้องเป็นไปตาม codable codable และ hashable hashable โปรโตคอล เราใช้วิธีการต่อไปนี้ในการจัดการวัตถุเหล่านี้ในฐานข้อมูล back4app create //when creating a new instance of author we use 1 var newauthor author = author(name "john doe") 2 3 // saves newauthor on your back4app database synchronously and returns the new saved item it throws and error if something went wrong 4 let savedauthor = try? newauthor save() 5 6 // saves newauthor on your back4app database asynchronously, and passes a result\<author, parseerror> object to the completion block to handle the save process 7 newauthor save { result in 8 // handle the result to check wether the save process was successfull or not 9 } read //for reading any of the objects introduced above, we construct the corresponding queries for each of them for author we have 1 let authorquery = author query() // a query to fetch all author items on your back4app database 2 3 // fetches the items synchronously or throws an error if found 4 let fetchedauthors = try? query find() 5 6 // fetches the items asynchronously and calls a completion block passing a result object containing the result of the operation 7 query find { result in 8 // handle the result 9 } add relations //in 1 1 relations, it is sufficient to have the child object as a property of the parent object back4app automatically saves the child object when the parent object is saved for the remaining relations, we use the add( objects ) method via the relation property available in the parent parseobject adding a relation called authors on a book object would look like this 1 let somebook book 2 let authors \[author] 3 4 // adds the relation between somebook and authors under the name 'authors' 5 let booktoauthorsrelation = try? somebook relation? add("authors", objects authors) 6 7 // saves the relation synchronously 8 let updatedsomebook = try? booktoauthorsrelation save() 9 10 // saves the relation asynchronously 11 booktoauthorsrelation save { result in 12 // handle the result 13 } query relations //for 1 1 relations, it is enough to append the include() method in the query for instance, to query all the books together with their isbn relation, we use 1 var query = book query() include("isbn") 2 3 // fetches all books synchronously 4 let books = try? query find() 5 6 // fetches all books asynchronously 7 query find { result in 8 // handle the result 9 } remaining relations //for the remaining relations, we create a query by using the static method queryrelation( ,parent ) provided by the parseobject protocol querying the authors related to a book can be implemented in the following way 1 let somebook book 2 let authors \[author] 3 4 5 // we create a relation (identified by the name 'authors') betwee somebook and a set of authors 6 let booktoauthorsrelation = 7 guard let booktoauthorsrelation = try somebook relation? add("authors", objects authors) // book > author 8 else { 9 fatalerror("failed to add relation") 10 } 11 12 let savedrelation = try booktoauthorsrelation save() // saves the relation synchronously 13 14 booktoauthorsrelation save { result in // saves the relation asynchronously 15 // handle the result 16 } 1 ดาวน์โหลดเทมเพลตแอปหนังสือ โครงการ xcode xcode มีโครงสร้างดังต่อไปนี้ ในทุกช่วงเวลา คุณสามารถเข้าถึงโครงการทั้งหมดได้ผ่านทาง github repositories ของเรา ios ตัวอย่าง repository 2 กระบวนการ crud เพิ่มเติม ก่อนที่จะไปต่อ จำเป็นต้อง implement ฟังก์ชัน crud บางอย่างเพื่อให้เราสามารถบันทึก ผู้เขียน ผู้เขียน , ผู้เผยแพร่ ผู้เผยแพร่ และ ประเภท ประเภท วัตถุ ในไฟล์ maincontroller+parseswift swift maincontroller+parseswift swift ซึ่งอยู่ภายใต้ extension สำหรับ maincontroller maincontroller คลาส เราได้ implement วิธีการต่อไปนี้ 1 // maincontroller+parseswift swift file 2 extension maincontroller { 3 /// collects the data to save an instance of book on your back4app database 4 func savebook() { 5 view\ endediting(true) 6 7 // 1 first retrieve all the information for the book (title, isbn, etc) 8 guard let booktitle = booktitletextfield text else { 9 return presentalert(title "error", message "invalid book title") 10 } 11 12 guard let isbnvalue = isbntextfield text else { 13 return presentalert(title "error", message "invalid isbn value ") 14 } 15 16 let query = isbn query("value" == isbnvalue) 17 18 guard (try? query first()) == nil else { 19 return presentalert(title "error", message "the entered isbn already exists ") 20 } 21 22 guard let genreobjectid = genreoptionsview\ selectedoptionids first, 23 let genre = genres first(where { $0 objectid == genreobjectid}) 24 else { 25 return presentalert(title "error", message "invalid genre ") 26 } 27 28 guard let publishingyearstring = publishingyeartextfield text, let publishingyear = int(publishingyearstring) else { 29 return presentalert(title "error", message "invalid publishing year ") 30 } 31 32 let authors \[author] = self authoroptionsview\ selectedoptionids compactmap { \[weak self] objectid in 33 self? authors first(where { objectid == $0 objectid }) 34 } 35 36 let publishers \[publisher] = self publisheroptionsview\ selectedoptionids compactmap { \[weak self] objectid in 37 self? publishers first(where { objectid == $0 objectid }) 38 } 39 40 // since we are making multiple requests to back4app, it is better to use synchronous methods and dispatch them on the background queue 41 dispatchqueue global(qos background) async { 42 do { 43 let isbn = isbn(value isbnvalue) // 2 instantiate a new isbn object 44 45 let savedbook = try book( // 3 instantiate a new book object with the corresponding input fields 46 title booktitle, 47 publishingyear publishingyear, 48 genre genre, 49 isbn isbn 50 ) save() // 4 save the new book object 51 52 // here we will implement the relations 53 54 dispatchqueue main async { 55 self presentalert(title "success", message "book saved successfully ") 56 } 57 } catch { 58 dispatchqueue main async { 59 self presentalert(title "error", message "failed to save book \\((error as! parseerror) message)") 60 } 61 } 62 } 63 } 64 65 /// retrieves all the data saved under the genre class in your back4app database 66 func fetchgenres() { 67 let query = genre query() 68 69 query find { \[weak self] result in 70 switch result { 71 case success(let genres) 72 self? genres = genres // when setting self? genres, it triggers the corresponding ui update 73 case failure(let error) 74 self? presentalert(title "error", message error message) 75 } 76 } 77 } 78 79 /// presents a simple alert where the user can enter the name of a genre to save it on your back4app database 80 func handleaddgenre() { 81 // displays a form with a single input and executes the completion block when the user presses the submit button 82 presentform( 83 title "add genre", 84 description "enter a description for the genre", 85 placeholder nil 86 ) { \[weak self] name in 87 guard let name = name else { return } 88 let genre = genre(name name) 89 90 let query = genre query("name" == name) 91 92 guard ((try? query first()) == nil) else { 93 self? presentalert(title "error", message "this genre already exists ") 94 return 95 } 96 97 genre save { \[weak self] result in 98 switch result { 99 case success(let addedgenre) 100 self? presentalert(title "success", message "genre added!") 101 self? genres append(addedgenre) 102 case failure(let error) 103 self? presentalert(title "error", message "failed to save genre \\(error message)") 104 } 105 } 106 } 107 } 108 109 /// retrieves all the data saved under the publisher class in your back4app database 110 func fetchpublishers() { 111 let query = publisher query() 112 113 query find { \[weak self] result in 114 switch result { 115 case success(let publishers) 116 self? publishers = publishers 117 case failure(let error) 118 self? presentalert(title "error", message error message) 119 } 120 } 121 } 122 123 /// presents a simple alert where the user can enter the name of a publisher to save it on your back4app database 124 func handleaddpublisher() { 125 // displays a form with a single input and executes the completion block when the user presses the submit button 126 presentform( 127 title "add publisher", 128 description "enter the name of the publisher", 129 placeholder nil 130 ) { \[weak self] name in 131 guard let name = name else { return } 132 133 let query = publisher query("name" == name) 134 135 guard ((try? query first()) == nil) else { 136 self? presentalert(title "error", message "this publisher already exists ") 137 return 138 } 139 140 let publisher = publisher(name name) 141 142 publisher save { \[weak self] result in 143 switch result { 144 case success(let addedpublisher) 145 self? presentalert(title "success", message "publisher added!") 146 self? publishers append(addedpublisher) 147 case failure(let error) 148 self? presentalert(title "error", message "failed to save publisher \\(error message)") 149 } 150 } 151 } 152 } 153 154 /// retrieves all the data saved under the genre class in your back4app database 155 func fetchauthors() { 156 let query = author query() 157 158 query find { \[weak self] result in 159 switch result { 160 case success(let authors) 161 self? authors = authors 162 case failure(let error) 163 self? presentalert(title "error", message error message) 164 } 165 } 166 } 167 168 /// presents a simple alert where the user can enter the name of an author to save it on your back4app database 169 func handleaddauthor() { 170 // displays a form with a single input and executes the completion block when the user presses the submit button 171 presentform( 172 title "add author", 173 description "enter the name of the author", 174 placeholder nil 175 ) { \[weak self] name in 176 guard let name = name else { return } 177 178 let query = author query("name" == name) 179 180 guard ((try? query first()) == nil) else { 181 self? presentalert(title "error", message "this author already exists ") 182 return 183 } 184 185 let author = author(name name) 186 187 author save { \[weak self] result in 188 switch result { 189 case success(let addedauthor) 190 self? presentalert(title "success", message "author added!") 191 self? authors append(addedauthor) 192 case failure(let error) 193 self? presentalert(title "error", message "failed to save author \\(error message)") 194 } 195 } 196 } 197 } 198 }sw สำหรับรายละเอียดเพิ่มเติมเกี่ยวกับขั้นตอนนี้ คุณสามารถไปที่ คู่มือการดำเนินการพื้นฐาน 3 ตั้งค่าความสัมพันธ์ ก่อนเริ่มสร้างความสัมพันธ์ ให้ดูที่ การอ้างอิงอย่างรวดเร็ว https //www back4app com/docs/ios/parse swift sdk/data objects/relationships#quick reference เพื่อให้เข้าใจเกี่ยวกับวัตถุที่เราต้องการเชื่อมโยงกัน ในภาพด้านล่างเราจะแสดงให้เห็นว่าวัตถุเหล่านี้มีความสัมพันธ์กันอย่างไร จากที่เห็น ความสัมพันธ์ถูกสร้างขึ้นโดยการวาง หนังสือ หนังสือ วัตถุไว้ตรงกลาง ลูกศรแสดงให้เห็นว่าวัตถุแต่ละตัวมีความสัมพันธ์กับ หนังสือ หนังสือ วัตถุอย่างไร 4 การนำความสัมพันธ์ไปใช้ กรณี 1 1 การเพิ่ม 1 1 ความสัมพันธ์สามารถทำได้ง่ายโดยการเพิ่มคุณสมบัติใน หนังสือ หนังสือ วัตถุ, เช่น, 1 struct book parseobject { 2 3 4 var isbn isbn? // esablishes a 1 1 relation between book and isbn 5 6 7 } ในกรณีนี้, หนังสือ หนังสือ และ isbn isbn มีความสัมพันธ์แบบ 1 1 โดยที่ หนังสือ หนังสือ ถูกระบุว่าเป็นผู้ปกครองและ isbn isbn เป็นบุตร ในภายใน เมื่อ back4app บันทึกอินสแตนซ์ของ หนังสือ หนังสือ , มันจะบันทึก isbn isbn วัตถุ (ภายใต้ชื่อคลาส isbn isbn ) ก่อน หลังจากกระบวนการนี้เสร็จสิ้น back4app จะดำเนินการต่อด้วยวัตถุ หนังสือ หนังสือ วัตถุ หนังสือ หนังสือ ใหม่จะถูกบันทึกในลักษณะที่ isbn isbn คุณสมบัติจะถูกแทนที่ด้วย pointer\<isbn> pointer\<isbn> วัตถุ วัตถุ pointer<> pointer<> ช่วยให้เราสามารถเก็บอินสแตนซ์ที่ไม่ซ้ำกันของ isbn isbn วัตถุที่เกี่ยวข้องกับผู้ปกครองที่สอดคล้องกัน กรณี 1\ n สำหรับ 1\ n ความสัมพันธ์ วิธีที่มีประสิทธิภาพที่สุดในการนำไปใช้คือผ่าน parserelation\<book> parserelation\<book> อ็อบเจ็กต์ parseswift มีชุดของวิธีการในการเพิ่มประเภทความสัมพันธ์เหล่านี้สำหรับอ็อบเจ็กต์ใด ๆ ที่สอดคล้องกับ parseobject parseobject โปรโตคอล ตัวอย่างเช่น หากเราต้องการสร้าง 1\ n 1\ n ความสัมพันธ์ระหว่าง book book และ author author เราสามารถใช้ 1 let somebook book 2 let authors \[author] 3 4 5 // we create a relation (identified by the name 'authors') between somebook and a set of authors 6 let booktoauthorsrelation = 7 guard let booktoauthorsrelation = try somebook relation? add("authors", objects authors) // book > author 8 else { 9 fatalerror("failed to add relation") 10 } 11 12 let savedrelation = try booktoauthorsrelation save() // saves the relation synchronously 13 14 booktoauthorsrelation save { result in // saves the relation asynchronously 15 // handle the result 16 } มันง่ายที่จะปรับเปลี่ยนโค้ดนี้สำหรับความสัมพันธ์อื่น ๆ ที่เรามีสำหรับ หนังสือ หนังสือ \ รวมทุกอย่างเข้าด้วยกัน เมื่อเรากำหนดแนวคิดพื้นฐานในการดำเนินการความสัมพันธ์แล้ว ตอนนี้เราจะทำให้ savebook() savebook() เสร็จสมบูรณ์ เราจะระบุจุดสำคัญที่ต้องคำนึงถึงในระหว่างกระบวนการนี้ 1 extension maincontroller { 2 /// collects the data to save an instance of book on your back4app database 3 func savebook() { 4 5 // 1 first retrieve all the information for the book (booktitle, isbnvalue, etc) 6 7 8 // since we are making multiple requests to back4app, it is better to use synchronous methods and dispatch them on the background queue 9 dispatchqueue global(qos background) async { 10 do { 11 let isbn = isbn(value isbnvalue) // 2 instantiate a new isbn object 12 13 let savedbook = try book( // 3 instantiate a new book object with the corresponding input fields 14 title booktitle, 15 publishingyear publishingyear, 16 genre genre, 17 isbn isbn 18 ) save() // 4 save the new book object 19 20 // 5 add the corresponding relations for new book object 21 guard let booktoauthorsrelation = try savedbook relation? add("authors", objects authors), // book > author 22 let bootktopublishersrelation = try savedbook relation? add("publishers", objects publishers), // book > publisher 23 let genrerelation = try genre relation? add("books", objects \[savedbook]) // genre > book 24 else { 25 return dispatchqueue main async { 26 self presentalert(title "error", message "failed to add relations") 27 } 28 } 29 30 // 6 save the relations 31 = try booktoauthorsrelation save() 32 = try bootktopublishersrelation save() 33 = try genrerelation save() 34 35 dispatchqueue main async { 36 self presentalert(title "success", message "book saved successfully ") 37 } 38 } catch { 39 dispatchqueue main async { 40 self presentalert(title "error", message "failed to save book \\((error as! parseerror) message)") 41 } 42 } 43 } 44 } 45 46 47 } 5 การสอบถามความสัมพันธ์ สำหรับ 1 1 ความสัมพันธ์ โดยพิจารณาจากพ่อแม่ book book และลูกของมัน isbn isbn , เราจะสอบถามลูกที่เกี่ยวข้องโดยการรวมมันใน book book การสอบถาม 1 let query = book query() include("isbn") 2 3 let books = try query find() // retrieves synchronously all the books together with its isbn 4 5 query find { result in // retrieves asynchronously all the books together with its isbn 6 // handle the result 7 } 8 ด้วยสิ่งนี้ หนังสือทั้งหมดจาก query จะมีคุณสมบัติ isbn ถูกตั้งค่าอย่างถูกต้องพร้อมกับ isbn ที่เกี่ยวข้อง ในทางกลับกัน เพื่อดึงวัตถุที่มีความสัมพันธ์ 1\ n โปรโตคอล parseobject ให้วิธีการสแตติก queryrelation( ,parent ) โดยการให้ชื่อของความสัมพันธ์ (เป็นพารามิเตอร์แรก) และพ่อแม่ วิธีนี้ช่วยให้เราสามารถสร้างการค้นหาที่ต้องการได้ ตัวอย่างเช่น เพื่อดึง author ที่เกี่ยวข้องกับ book เฉพาะ เราสามารถใช้โค้ดต่อไปนี้ 1 let book book // book from which we are trying to retrieve its related authors 2 3 do { 4 let authorsquery = try author queryrelations("authors", parent book) // 'authors' is the name of the relation it was saved with 5 6 authorsquery find { \[weak self] result in 7 switch result { 8 case success(let authors) 9 self? authors = authors 10 11 dispatchqueue main async { 12 // update the ui 13 } 14 case failure(let error) 15 dispatchqueue main async { 16 self? presentalert(title "error", message "failed to retrieve authors \\(error message)") 17 } 18 } 19 } 20 } 21 } catch { 22 if let error = error as? parseerror { 23 presentalert(title "error", message "failed to retrieve authors \\(error message)") 24 } else { 25 presentalert(title "error", message "failed to retrieve authors \\(error localizeddescription)") 26 } 27 } ในทำนองเดียวกัน เราสามารถสอบถามวัตถุที่เกี่ยวข้องอื่น ๆ เช่น publisher ในไฟล์ bookdetailscontroller swift เราจะดำเนินการสอบถามเหล่านี้เพื่อแสดงความสัมพันธ์ที่หนังสือมีต่อผู้เขียนและผู้จัดพิมพ์ 1 // bookdetailscontroller swift file 2 class bookdetailscontroller uitableviewcontroller { 3 4 5 /// retrieves the book's details, i e , its relation with authors and publishers 6 private func fetchdetails() { 7 do { 8 // constructs the relations you want to query 9 let publishersquery = try publisher queryrelations("publishers", parent book) 10 let authorsquery = try author queryrelations("authors", parent book) 11 12 // obtains the publishers related to book and display them on the tableview, it presents an error if happened 13 publishersquery find { \[weak self] result in 14 switch result { 15 case success(let publishers) 16 self? publishers = publishers 17 18 // update the ui 19 dispatchqueue main async { 20 self? tableview\ reloadsections(indexset(\[section publisher rawvalue]), with none) 21 } 22 case failure(let error) 23 dispatchqueue main async { 24 self? presentalert(title "error", message "failed to retrieve publishers \\(error message)") 25 } 26 } 27 } 28 29 // obtains the authors related to book and display them on the tableview, it presents an error if happened 30 authorsquery find { \[weak self] result in 31 switch result { 32 case success(let authors) 33 self? authors = authors 34 35 // update the ui 36 dispatchqueue main async { 37 self? tableview\ reloadsections(indexset(\[section author rawvalue]), with none) 38 } 39 case failure(let error) 40 dispatchqueue main async { 41 self? presentalert(title "error", message "failed to retrieve authors \\(error message)") 42 } 43 } 44 } 45 } catch { // if there was an error during the creation of the queries, this block should catch it 46 if let error = error as? parseerror { 47 presentalert(title "error", message "failed to retrieve authors \\(error message)") 48 } else { 49 presentalert(title "error", message "failed to retrieve authors \\(error localizeddescription)") 50 } 51 } 52 } 53 54 55 } 6 รันแอป! ก่อนกดปุ่มรันบน xcode xcode , อย่าลืมตั้งค่าการใช้งาน back4app back4app ใน appdelegate appdelegate คลาส! คุณต้องเพิ่ม ประเภท สองสามประเภท, ผู้จัดพิมพ์ และ ผู้เขียน ก่อนที่จะเพิ่มหนังสือใหม่ จากนั้นคุณสามารถเริ่มป้อนข้อมูลหนังสือเพื่อบันทึกลงในฐานข้อมูล back4app ของคุณ เมื่อคุณบันทึกหนังสือหนึ่งเล่มแล้ว ให้เปิด แดชบอร์ด back4app https //parse dashboard back4app com/apps และไปที่แอปพลิเคชันของคุณที่เชื่อมโยงกับ xcode โปรเจกต์ ในส่วนฐานข้อมูล คุณจะพบคลาส book book ซึ่งเก็บหนังสือทั้งหมดที่สร้างโดย ios ios แอป นอกจากนี้ คุณจะเห็นว่า back4app ได้สร้างคลาส isbn isbn โดยอัตโนมัติเพื่อเชื่อมโยงกับ book book ที่เกี่ยวข้อง หากคุณกลับไปที่ book book คลาส คุณสามารถระบุประเภทข้อมูลสำหรับแต่ละประเภทของความสัมพันธ์ ในกรณีของ isbn isbn และ ประเภท ประเภท , ประเภทข้อมูลคือ pointer<> pointer<> ในทางกลับกัน สำหรับความสัมพันธ์เช่น ผู้เขียน ผู้เขียน และ ผู้จัดพิมพ์ ผู้จัดพิมพ์ , ประเภทข้อมูลคือ relation<> relation<> นี่คือความแตกต่างที่สำคัญที่ต้องคำนึงถึงเมื่อสร้างความสัมพันธ์