Android database operations with Room and RxAndroid — Easy threading

Krishan Madushanka
3 min readAug 5, 2021

--

Today’s topic’s main objective is to implement a db connection with a local database using Room and RxAndroid libraries in Android and try some database operations.

Before that I want to answer few questions.

What is RxJava/RxAndroid ?

RxJava is a Java based implementation of Reactive Programming.

RxAndroid is specific to Android platform which utilises some classes on top of the RxJava library.

What is Room ?

Simply Room is a persistence library .But with it you don’t have to use SQLiteOpenHelper and traditional SQLite interfaces that you used when implementing SQLite databases for your app.Because of that reason it reduces boilerplate code that you wrote earlier without Room.

Room has 3 major components called:

  • Database: Contains the database holder and serves as the main access point for the underlying connection to your app’s persisted, relational data.
  • Entity: Represents a table within the database.
  • DAO: Contains the methods used for accessing the database.

You have to implement those classes and interfaces when you work with Room.

Anyway that is enough about Room and RxJava. If you want to dig more about those libraries, you can refer, there are lot of articles in the internet. My main focus is to discuss the key benefit of using RxJava/RxAndroid with Room.

As you may have heard, with RxJava threading have become easy. If you have done asynchronous operations in Android you may have cursed about using AsyncTask.If you use Room without RxJava as well you have to use AsyncTask to make db operation since db operations should be done on a separate thread.Unless it will block the main(UI) thread. But with RxJava it is super easy to write code to do asynchronous operations.

From the code we are going to write today, you can learn how to create a simple db using Room and do some db operations with the help of RxJava.

First create a new project and add below dependencies to app level build.gradle file.

Create Item entity

Now create the Dao for this.This will be an interface annotated with @Dao annotation which contains methods to query above table.

Note that in above code return types are wrapped with “Single” which is used to return an observable when above methods are called. We want to get an observable here since we write codes to call these methods using RxJava below.

Create the Database class also.

This class is an abstract class which extends RoomDatabase class.Here you have to add your Entity class as entities = {Item.class} and declare ItemDao as well as abstract method. getAppDatabase() method will return AppDatabase type object which is created using Room.databaseBuilder() method.

Now let’s call the methods in Dao, using RxJava from our UI.

Above code demonstrates how to insert an Item to the table and the id will be returned to us inside onSuccess() method.

Note subscribeOn(Schedulers.io()) and observeOn(AndroidSchedulers.mainThread()).

SubscribeOn specify the Scheduler on which an Observable will operate. ObserveOn specify the Scheduler on which an observer will observe this Observable.

Simply you do the db call in a separate thread and observe it from main thread.

So basically SubscribeOn is mostly subscribed (executed) on a background thread ( you do not want to block the UI thread while waiting for the observable) and also in ObserveOn you want to observe the result on a main thread.

If you are familiar with AsyncTask then SubscribeOn is similar to doInBackground method and ObserveOn to onPostExecute.

Easy ah?

Find the full code from here.

Hit on clap if this helped you and follow me for more articles.

--

--

Krishan Madushanka

Software Engineer | Android | iOS | Flutter | React Native | IoT | aws certified