Offline App with RxJava 2 and Room

Mert SIMSEK
3 min readJul 11, 2017

Offline apps are so popular nowadays. It is really important to implement offline support to your app and it is getting more and more inevitable. Couple of months ago, some engineers work @google shared an android architecture repository on github. They showed us how ViewModel, LiveData, LifeCycle etc. should be used in our android apps. These components are really good to reduce boilerplate code and improve your app’s code quality though. I already shared a repo that implements all of these LiveData, ViewModel, Room. You can check my repo on github.I used LiveData and Room for offline support.

I know the power of RxJava, and in this blogpost I am gonna talk about how we can combine RxJava 2 (instead of LiveData) and Room to make an offline app.

Single Source of Truth (SSOT)

If you want to make your app offline, you should follow SSOT rule first. What is that? Glad you asked.

SSOT refers to the concept where certain data has only one official source to be used by data consumers (i.e. humans and software) for the true current version of that data.

We should trust to one source. It will be the Room DB in this case. Your Room Database serves as the single source of truth, and other parts of the app access it via the repository.

Goal

  • ViewModel call repository method and doesn’t care about how data is retrieved.
  • Repository cares about fetching, saving, updating, streaming data.
  • Repository needs Dao for saving, updating and querying data.
  • Repository needs RemoteService(Retrofit, firebase etc.) to fetch fresh data.

To-Do for the Goal

  • Repository queries from DB and send it to ViewModel immediately (with Loading state).
  • Repository fetches from remote and insert it into DB
  • Repository queries from DB and send it to ViewModel (with Success state)

Dependencies

ArchRoom & ArchRoomCompiler : These dependencies let us use room db.

ArchLifecycleReactiveStreams : We want to survive from rotation. This is why we use reactivestreams to convert flowable to livedata in ViewModel.

ArchRoomRx : Thanks to people who wrote this. When we insert into DB, You Flowable stream will be notified automagically. It means that you don’t have to refresh your list to fetch again. If you start listening once, you get every update on your DB table. (No more dispatch event -between fragments-)

Sample

Currently I am working on a radio application. I will show you “genres” part as offline feature.

Dao and Service

I am not going to deep dive into Dao and Firebase classes. Mainly, I want to show you applying offline support using RxJava and Room. If you have any question about Room or RxJava, please don’t hesitate to comment.

Repository

I will do offline support operation in most of the features of my app. I created a NetworkBoundResource abstract class to use it whenever I want to persist data offline.This class is built for applying same operation with given DAO and service method. In google sample app, they already gave us a hint how to do that with LiveData. I just convert it to RxJava 2.

My remote data model and local data model are different from each other. That is why I use mapper abstract method. It gets mapper implementation from subclass.

ViewModel

I am just calling radioRepository.getGenres(), Flowable emits old data when old data is ready. And emits fresh data when new data is inserted to DB.

Fragment or Activity

I subscribed to channel and I update the UI when data is arrived.

Summary

I didn’t want to explain Room and RxJava entirely. Maybe next blogpost will be about that. But In this blogpost, I wanted to show you how you can combine your Room and RxJava 2 to make your app offline. I am not advanced in RxJava 2. I mean, That would be great to hear from you if you have a suggestion.

Happy coding.

--

--