Relationships
Relations are a fundamental feature for organizing data objects stored on a database. ParseSwift does provide the necessary tools and methods to establish relations between classes in your Back4App Database. Depending on the use case, we can identify the following type of relations
- 1:1: A relation that only connects two data objects.
- 1:N: A relation between one data object andNdata objects
- N:N: A relation beweenNdata objects toNdata objects.
As we will see below, implementing 1:1 relations are relatively straightforward. For 1:N and N:N relations, the implementation involves the ParseRelation object provided by ParseSwift SDK. There are additional alternatives for implementing 1:N and N:N relations. However, due to efficiency and performance, we recommend following our approach
This tutorial uses a basic app created in Xcode 12 and iOS 14.
At any time, you can access the complete Project via our GitHub repositories.
- To understand how relations are implemented in a Back4App Database.
To complete this quickstart, you need:
- Xcode.
- An app created at Back4App.
- Note: Follow the Install Parse SDK (Swift) Tutorial to create an Xcode Project connected to Back4App.
The project template is a Book App where the user enters a book details to save it on a Back4App Database. On the app’s homescreen you will find the form to do so
Using the + button located on the top-right side of the navigation bar, we can add as many Publishers, Genres and Authors as needed. Once the user enters a book, they can use the Add book button to save the book on their Back4App Database. Additionally, the List books button will allow us to show all the books the user added and also to see their relationship with the publishers and authors.
We make use of the objects Author, Publisher, ISBN and Book:
Before storing instances of these objects in a Back4App Database, all their properties must conform to the Codable and Hashable protocols.
We make use of the following methods for managing these objects on the Back4App Database:
The XCode project has the following structure
At any time, you can access the complete Project via our GitHub repositories.
Before going further, it is necessary to implement some CRUD functions for us to be able to save the Author, Publisher and Genre objects. In theMainController+ParseSwift.swift file, under an extension for the MainController class, we implemented the following methods
For more details about this step, you can go to the basic operations guide.
Before starting to create relations, take a look at the quick reference section to have an idea about the objects we want to relate to each other. In the figure below we show how these objects are related
As can be seen, the relations are created by putting the Book object in the middle. The arrows show how each object is related to a Book object.
1:1 case
Adding 1:1 relations can easlity be achieved by adding a property in the Book object, i.e.,
In this case, Book and ISBN share a 1:1 relation where Book is identified as the parent and ISBN as the child. Internally, when Back4App saves an instance of Book, it saves the ISBN object (under the ISBN class name) first. After this process is complete, Back4App continues with the object Book. The new Book object is saved in a way that its isbn property is represented by a Pointer<ISBN> object. A Pointer<> object allows us to store a unique instance of the ISBN object related to its corresponding parent.
1:N case
For 1:N relations, the most efficient way to implement them is via a ParseRelation<Book> object. ParseSwift provides a set of methods to add these types of relationships for any object conforming to the ParseObject protocol. For instance, if we want to create a 1:N relation between Book and Author we can use
It is straightforward to adapt this snippet for the other relations we have for Book.
- Putting all together
Once we established the basic idea to implement relations, we now complete the saveBook() method. We enumerate the key points to have in mind during this process
For 1:1 relations, given the parent Book and its child ISBN, we query the corresponding child by including it in the Book query
With this, all the books from the query will also have the isbn property set properly with the related ISBN object.
On the other hand, in order to retrieve objects with a 1:N relation, the ParseObject protocol provides the static method queryRelation(_,parent:). By providing the name of the relation (as the first parameter) and the parent, this method allows us to create the required query. For instance, to retrieve all the Author’s related to a specific Book we can use the following snippet
Similarly, we can query other related objects such as Publisher.
In the BookDetailsController.swift file we implement these queries to display the relation a book has with authors and publishers
Before pressing the run button on XCode, do not forget to configure your Back4App application in the AppDelegate class!
You have to add a couple of Genre’s, Publisher’s and Author’s before adding a new book. Then, you can start entering a book’s information to save it on your Back4App Database. Once you have saved one Book, open your Back4App dashboard and go to your application linked to the XCode project. In the Database section, you will find the class Book where all the books created by the iOS App are stored.
Additionally, you can see that Back4App automatically created the class ISBN in order to relate it with its corresponding Book object. If you go back to the Book class, you can identify the data types for each type of relation. In the case of ISBN and Genre, the data type is a Pointer<>. On the other hand, for relations like Author and Publisher, the data type is Relation<>. This is a key difference to have in mind when constructing relations.