Please share with small businesses around you to help them keep going.

As life has changed dramatically in the past few weeks, plans on business and personal levels are also changing by the minute. Traditional conventional ways of communicating are now a risk to lives…

Smartphone

独家优惠奖金 100% 高达 1 BTC + 180 免费旋转




Android Room Database Library with Coroutines Example

Room is a database library and it is one of the Android Architecture Components. It provides an abstraction layer over SQLite to allow for more robust database accesses.

In this article, we are going to enhance the app developed in the above article. The current app fetches a quote from REST API and shows it in UI. The enhancement in this article is going to store the quote in the SQLite database after REST API execution and before showing it in UI.

The Room library is just wrapper over the SQLite database, but it solves the following major problems.

There are three major components in the Room.

Entities:

DAO:

Database:

Let’s create these components for our app in the upcoming sections.

The model class used for Retrofit is reused as a Room entity class. Just a couple of annotations added additionally.

@Entity annotation added for a class to let Room know this class objects need to persist in the SQLite database.

@PrimaryKey annotation added for the id field to represent the primary key column of a table.

For the above entity class, the Room library creates a table called quotes with three columns id, text and author in the database it is going to associate.

The QuotesDao interface defines methods to access data from the quotes table. For our current requirement, we only need an insert method for storing a quote fetched from REST API. We will add more methods here in the future while further enhancing the app.

As we have added @Insert annotation this method, while this method is getting called Room library just take the values from the model object and insert it in the quotes table.

Another thing to note is, suspend modifier added in the method definition. It is because android can’t allow us to do database insert operation in the main thread and we will invoke this method from Coroutines in this article later.

The AppDatabase class defines the entry point component of the Room. The followings are the key points to note in this class.

At this point we are done with Room database library components set up in our app. In the next section let’s use this in ViewModel and store the quotes fetched from REST API.

In ViewModel’s fetchRandomQuote method only addition we need is quotesDao.insertQuote(quote) as we have already done the API call execution in a coroutine.

But how to get quotesDao instance is a question here. Have a look at the below code for it.

We have defined the companion object for AppDatabase and getter method for the same already in the previous section. Here we have to use it to get AppDatabase instance and then the quotesDao object from it.

With all the above changes our RandomQuoteViewModel class looks like below.

In the activity class, just need to modify the code which is used for getting ViewModel objects.

We have to use AndroidViewModelFactory for getting RandomQuoteViewModel object as it is now a child of AndroidViewModel class and it expects the Application object as a constructor argument.

All set now, every time quotes is fetched from REST API it will be stored in a database. But how can we verify database insert operation happened successfully and quotes are saved in the database table. Let’s see it next article.

Add a comment

Related posts:

STABLECOIN DIGEST 29.03.2020

The European Association of CCP Clearing Houses (EACH) has responded to the European Commission Public Consultation “On an EU framework for markets in crypto-assets”. The entity underlined the need…