Relationships
Using Parse, you can store data objects establishing relations between them. To model this behavior, any ParseObject can be used as a value in other ParseObject. Internally, the Parse framework will store the referred-to object in just one place, to maintain consistency. That can give you extra power when building and running complex queries. There are three main relation types:
- one-to-one, establishing direct relations between two objects and only them;
- one-to-many, where one object can be related to many other objects;
- many-to-many, which can create many complex relations between many objects.
There are two ways to create a one-to-many relation in Parse:
- The first is using the Pointers in Child Class, which is the fastest in creation and query time.
- The second is using Arrays of Pointersin Parent Class which can lead to slow query times depending on their size. Because of this performance issue, we will use only pointers examples.
There are three ways to create a many-to-many relation in Parse.
- The first is using the Parse Relations, which is the fastest in creation and query time. We will use this in this guide.
- The second is using Arrays of Pointers which can lead to slow query times depending on their size.
- The third is using JoinTablewhere the idea from classical database. When there is a many-to-nany relation, we combine every objectId or Pointer from both sides together to build a new separate table in which the relationship is tracked.
This tutorial uses a basic app created in Android Studio 4.1.1 with buildToolsVersion=30.0.2 , Compile SDK Version = 30.0.2 and targetSdkVersion 30
At any time, you can access the complete Android Project built with this tutorial at our Github repositories
Our goal is, understand Parse Relations by creating a practical Book app.
Here is a preview of what we are gonna achieve:
To complete this tutorial, we need:
- An app created on Back4App.
- An android app connected to Back4App.
- Note: Follow the Install Parse SDK tutorial to create an Android Studio Project connected to Back4App.
The main object class you’ll be using is the Book class, storing each book entry in the registration. Also, these are the other three object classes:
- Publisher: book publisher name, one-to-many relation withBook
- Genre: book genre, one-to-many relation withBook. Note that for this example we will consider that a book can only have one genre;
- Author: book author, many-to-many relation withBook, since a book can have more than one author and an author can have more than one book as well;
A visual representation of these data model:
Before next steps, we need to connect Back4App to our application. You should save the appId and clientKey from the Back4App to string.xml file and then init Parse in our App.java or App.kt file. Follow the New Parse App tutorial if you don’t know how to init Parse to your app.
Or you can download the projects we shared the github links above and edit only the appId and clientKey parts according to you.
In this step we will see how to save and list the Genres, Publishers and Authors classes related with the Book class.
We can register aGenreusing the following snippet.
We can register aGenreusing the following snippet.
We can register aPublisherusing the following snippet.
We can register aPublisherusing the following snippet.
We can register aAuthorusing the following snippet.
We can register aAuthorusing the following snippet.
In this part, we use a model class named ParseObjectModel. In this model class, we have a ParseObject variable to be able to read the data, and the isChecked variable, which we will use to save the book in the next step. We will be able to easily retrieve the selected objects with the isChecked variable. Here is the our ParseObjectModel model.
This function will create a new Book in Back4app database with 1:N relations.
This function will create a new Book in Back4app database with N:N relations. For the Author relation, we find the selected Author/s in the adapter of the authorRecyclerView and save them as Parse Relation.
With these functions, we will list our Books according to their Publishers. First, we throw a query to the Publisher class.
And then we query to list the Books that each Publisher item is related to.
Now, when we click on any Book object, we send the Object Id of this Book with an intent to the page that will show the details of that Book. And we get all the details of the Book from the database by using this Object Id on that page.
At this point, we have learned Parse Relationships on Android.