Room: the pleasure of having a simple and observable database on Android

Rygel Louv

Couple months ago i decided to start working with Room persistence library which is one of the components of Android Architecture Components. Before that, i did a small benchmark to compare between existing solutions. My specs were quite clear. I wanted it to be/have:

  • Simple to use with a very intuitive way to access the data
  • Observable so that it can notify the presentation layer for the changes i wanted to keep an eye on. And this was the most important for me.
  • No inheritance to some class on my entity models
  • A satisfying speed

So my benchmark included SnappyDB, GreenDAO from GreenRobot, ObjectBox also from GreenRobot and which is supposed to be the new upgrade of GreenDAO, Realm that you probably heard about already, ActiveAndroid which i have already used in the past, and Room . This article is not about comparing these great tools because by making some research you will find great articles dealing with the subject like this one. But if you give a try to each of them and filter them against the specs i stated above, you will probably make your own idea. For me, most of them did not met my requirements as i wanted. Some of them forced me a certain behavior, others had compilation issues etc… Again, don’t get me wrong, these tools are all great but yet, they all still have their pros and cons.

Anyway, the one i finally picked out of this was Room. Even though it is not the best in terms of speed, it meets all the other requirements. And as bonus, it has two advantages that i really love:

  • It is fully compatible with MVVM-AAC architecture and can return LiveData
  • It is also compatible with RxJava2 and can return things like Flowable, Single, Maybe etc…

So the ability of Room to return LiveData, makes it a really powerful ORM for SQLite. But if you are a RxJava guy, as they use to say nowadays “Rx is the new cool”, You can definitely return RxJava stuff. By doing that, you can have a clean way to build your app by having the data access layer of your app using Rx and the presentation layer using LiveData. This tweet from the great Googler Chris Banes explains it better:

At this moment, you are probably asking yourself (or asking me) the question: “how will LiveData and Rx communicate ?” 😏 Well, this is the best part. In fact, Android in it’s Architecture Components has this great class called LiveDataReactiveStream that can simply convert a LiveData into a Publisher (like RxJava2’s Flowable) and in the opposite way, a Publisher into LiveData.

What does it really mean ??

What does it mean ? It means each time your Room Query, that is actually returning a Flowable, detects a change, the stream (i mean the result of the request) is transmitted to the LiveDataReactiveStream that will automagically convert it into a LiveData and returns back the result to the presentation layer. In our case, it can be the ViewModel that made the call. The LiveData returned, which is actually living in the ViewModel, is then observed from the View (Fragment/Activity) that means there is an active observer, then the changes will be propagated and it will be your job to refresh the UI.

Maybe i talk too much

In your DAO:

@Query("SELECT * FROM posts")
Flowable<List<Post>> getAll();

In your Repository:

public LiveData<List<Post>> getPostListStream()
{
LiveDataReactiveStreams.fromPublisher(mDAO.getAll());
}

In your ViewModel you have:

public LiveData<List<Post>> getPostList()
{
return MyRepository.getInstance().getPostListStream();
}

And finally in your Fragment/Activity:

mViewModel.getPostList().observe(this, posts -> {
// TODO do the UI job here you genius! Refresh the UI!
// I hope you are familiar with lambdas in Java
}

By doing this, you have a simple ORM which is easy to work with, that is notifying you every time your post list is updated.

Even though Room is a great library, i still don’t like some of its features like the fact that i have to write SQL queries to access the data… i hate SQL but that is another story.

Destroy the clap button if you found this useful and do not hesitate to share as well. And if i did some mistakes here, it would be my pleasure to learn from you because i really believe that learning from people is the best way to learn.

Rygel Louv

Written by

Android developer. Passionate about Software Engineering, AI, and Machine Learning.

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade