Rx2Firebase : Firebase + RxJava [Android]

Francisco García Sierra
ProAndroidDev
Published in
3 min readNov 10, 2017

--

Today I’m going to talk about Rx2Firebase, a Rxjava 2.0 wrapper on Google’s Android Firebase library.

Everyday a lot of people work with Firebase , mobile and web developers… Almost any developer that stays up-to-date with the latest news in their respective communities knows about it, and has possibly used it to some extent. The use may just be limited to Crash Reporting or Analytics, although, most probably, most developers are using all its potential with Firebase Storage, Realtime Database, Admob, Test lab..

Around 2 years ago, I started using Firebase Realtime Database as a non-relational database in one of my personal mobile application projects. After understanding how the API works and setting aside my thoughts about traditional relational databases, I got down to work and mounted the entire data repository of my application. Part of my code used to look like this:

According to this code, in Firebase for every call to recover data from the server I had to implement a new interface and override the right methods. This generates a lot of boilerplate code, which got to the point of being overwhelming when a repository had tens of methods.

On the other hand, server calls became more chaotic in those cases where I decided not to denormalize my information, thus having to use nested queries to recover the desired data:

Nested callbacks again and again…I think that none of us likes this kind of things in our code, right? This pain was partially mitigated when I added retrolambda to my project, but it didn’t fully solve the main problem.

At the time, I was using RxJava 2.0 together with Clean arquitecture, so I thought about dedicating some of my time to wrap all my repository calls in reactive calls. In this way Rx2Firebase was born.

Rx2Firebase

At this point in this article I’m assuming that you are familiar with RxJava and the observer pattern. If you are not, I’d suggest that you take a break and investigate the matter, as it will open a new world of possibilities in front of you.

Below I show the examples mentioned above using Rx2Firebase instead of the conventional API:

It is much simpler and easier to read, right? There are thousands of examples like this one. All the methods of the Storage, FirebaseDatabase, Authentication and FirebaseFirestore (beta) APIs are wrapped with their respective reactive function in the library.

If you have previously worked with RxJava before, you will easily see all the possibilities that you can achieve using maps and flatmaps. Creating nested queries over your DataSnapshots or cast them to their respective POJO’s are just a few examples. In the same way, you will be able to use zip or merge to concat multiple database calls in a single disposable, returning them all at the same time.

You can grab Rx2Firebase by adding dependency to your build.gradle file:

compile 'com.github.frangsierra:rx2firebase:1.1.3'
//If you are using Firestore in your project
compile 'com.github.frangsierra:rx2firebase:1.4-beta.2'

These are just a few examples of how to use the library, feel free to use it, open issues, creating pull requests and contribute to the project.

Join us on GitHub, test out the API and let’s work together to make Firebase even more awesome! Happy coding!

--

--