Integrating your android app with Room Persistence Library in Kotlin

Muhammad Rahmatullah
4 min readAug 14, 2018

--

unsplash.com

When you’re developing android apps, often times you needed to save or store data in offline way. There are many options to achieve this whether using sharedpreference, Sqlite or else. You might consider to use Sharedpreference when your data is not that complex or it just need to store single set of value but when you need to store or save complex data relationship or it comes to a lot of data, the appropriate way to do it is using provided native sdk of android which is Sqlite.

Using Sqlite is actually could be little intimidating at first place since the configuration itself is sometimes overwhelming xD and will produce more boilerplate. Out there, there are many alternatives we can use to save or store data in our application in a convenient way. these are like :

Above choices of course has their own advantages and disadvantages.

As the post title, I am going to talk about using Room Persistence library in android app. I think room is very helpful and must have knowledge since this library from the Android team itself meaning that this library will have a great support and will always have an update. Moreover this is part of the AAC.

To use room, there are three main components that we need to know first. As any other library, first we need to configure it before we can use it. But no worries room configuration is not complicated as Sqlite heheh. Those components are :

  1. Entity : Entity is a class used to represent the table of the database. here we also declare the columns that we need.
  2. Database : Database is a class that extends RoomDatabase, in this class we declare what entity/table that we are going to use and also the version of the database.
  3. DAO : DAO or Data access object is an interface for accessing the data in the database, such as doing query to get the data, insert new rows, updating rows and else.
talk is cheap, show me the code :)

Okayy, let’s stop the theories xD, now let’s dive into the code.

To use room, we need this dependency

Then we add the class for the entity/table. By using kotlin the code will be very concise :)

Next step is adding DAO for accessing the database

Alright, the last step of the configuration phase is adding the Database class

Now we’re done with the configuration yayy. Ow yeah thing to be noticed that to do all the operation related to accessing the database such as get all the data, insert data and else, we need to do it outside the main thread and this is a default rule of the room library itself. This because if we do database operation in main threads it can make the application become more slow and laggy. Eventhough Room provide us the allowMainThreadQueries() but this thing is not recommended. Therefore we need to do it outside the main thread. Actually there are many ways to do this such as using Asynctask, handler or using RxJava. In this post I will use RxJava :).

Now let’s take a look at the activity that accessing the database

If you can see getAllData() and insertToDb() using RxJava to accessing the database.

You can find the full source code of this app in this github repo, don’t forget to star if you find it helpful:

In the future I will try to integrate this repo with popular libraries in android such as Dagger2, LiveData (AAC) and else. Ow yeah, I will also try to use different approach such as MVP and MVVM. so stay tuned!

--

--