Room with RxJava and Dagger

Ashwini Kumar
4 min readJul 11, 2018


A little long but nonetheless far away. The last article in the series of articles to move to MVVM design using Android Architecture Components, is available. You can read the other articles here:

  1. Migration from MVP to MVVVM using Android Architecture Components
  2. Pagination using Paging Library with RxJava and Dagger

I discussed on the Why’s and How’s of Migration and also talked on the usage of Paging Library in my previous articles. Let’s dive deep into Room Persistence Library.


Room is a part of Android Jetpack Libraries developed to provide architectural guidance and help make it quick and easy to build great Android apps. It is built as an abstraction layer over SQLite to allow for more robust database access while harnessing the full power of SQLite.


  • No need of creating long contracts, content provider, or Data Access Objects(DAOs) to work with SQLite anymore. Eliminates the boilerplate big time.
  • Annotation based processing which detects flaws in SQL query at compile time.
  • In-house support for RxJava.
  • Heavenly when clubbed with LiveData and PagingLibrary.

Having got so many amazing features, let’s build something using Room. I will talk on Room with the help of Favourite Screen being developed on my TvMaze repository. On the home page of TvMaze app, the user can mark/unmark any show as favourite. All the favourited shows will be saved on database using Room which can be visible on the Favourite screen.


To save the favourite shows in DB, I created a database named tvmaze.db. The version needs to be updated whenever there is any change to any entity of the database. Just annotate the class with @Database , provide the entities and the database is ready to use.


The favourite shows are stored in the table favourite_shows. Every row of this table is represented by the entity FavoriteShow.class. By default, Room creates a column for each field that’s defined in the entity. If an entity has fields that you don’t want to persist, you can annotate them using @Ignore Annotate the POJO with @Entity to be used as a row in your table. Also, to persist a field, Room must have access to it. You can make a field public, or you can provide a getter and setter for it.

Data Access Object(DAO)

Database and Table are created. It is time to store/access the data from that table. DAO helps in storing/accessing data with the database by performing CRUD operations via the rules defined inside it. To store/access favourite show, I defined methods to insert/delete a show into the table. By accessing a database using a DAO class instead of query builders or direct queries, you can separate different components of your database architecture. A DAO can be either an interface or an abstract class. If it’s an abstract class, it can optionally have a constructor that takes a RoomDatabase as its only parameter. Room creates each DAO implementation at compile time.

As you can see, I have leveraged RxJava on Room by defining a method which returns a Single . The RxJava support on Room can be added by defining the dependency in your build.gradle file.

Time for Execution

All the Room related stuffs have been created but they are somehow scattered. Let’s connect the dots now.

Inject Everything: I need to have a single instance of Database and the same can be provided in any class whenever needed. Dagger helped in this cause. I created a module which will provide Database along with the Dao. Now, they can be provided to any class using Constructor/Field injection because Dagger knows how to construct those objects.

To have Separation of Concern(SOC), I created a repository class which will handle all database related operations via DAO.

As discussed earlier, my requirement was to display all the favourited shows on a Favourite Screen. This is how the data in/out flow looks like in the current implementation.

FavoritesShowViewModel takes on FavoriteShowsRepository which in-turn takes ShowsDao to fetch shows from database.

The list of shows fetched from database are stored in LiveData . This LiveData is being observed in activity to display the list on the screen.

The integration of Room to develop Favourites screen can be found below:

Let’s see the complete implementation demo on the TvMaze app.

Room has loads to offer and this is just the beginning. Not only that, you can do a lot more using the new set of libraries provided in Android Jetpack. The Android Architecture Components have hit the right chord in delivering a first class solution to all the design and lifecycle management problems in Android. Go ahead, Accelerate Your Development, Eliminate Boilerplate & Build High Quality and Robust App today.

The complete TvMaze repository is hosted on Github. Better yet, the whole codebase of TvMaze repo has Static Code Analysis integrated. Refer here:

Feel free to share your feedback/love on Twitter or Medium. #Share2Learn



Ashwini Kumar

🎯 Engineering towards excellence every single day.