use Rxjava instead of Event Bus libraries

In Android community, everyone is talking about RxJava and why we should use RxJava in Android projects. After start implementing RxJava in our Android application, we noticed that we don’t need Otto(or any other event bus libraries) implemented in our code base. In this blog post, I am going to show how we got rid of Otto in our code base, start using RxJava instead.

let me tell you how we implemented MVP pattern into our code base

When we started developing Radyoland app which is a radio streaming application, We decided to use MVP pattern to design our code base, project structure and etc. We have splitted into some layers (domain, model, app and etc.)

In the model layer, we have classes and interfaces for RESTful operations. In domain layer, we tried to implement our business logic for application so that we created some usecase classes.

Why did we use event bus library in the first place?

if you have more than one layer in your Android application, that means you need to transfer data between these layers. In our case, we think if we define a BusUtil class for both DataBus and UI Bus, we can easily transfer data between these layers(model, domain, presentation).

You can “subscribe” to and “unsubscribe” from specific events posted by the bus. Logic behind this method works like this way.

in our UsecaseController, PresenterImp classes, we post an event requesting data described in our REST implementation classes and subscribed to this event.

When we call an asynchronous data request method with a callback argument, We use the bus to post the result data and subscribes to the result event.

and post it to update the UI. The activity or fragment should subscribe to events in onResume() and unsubscribes in onPause().

This implementation technique works well. As we are good Android developers, always try find better ways to do. We found a way to get rid of all callback and subscribe methods. We decided to use RxJava and RxAndroid for our Android application codebase.

let me tell you how we implemented Rxjava

First of all, We needed to change all method return types in RestInterface.

without RxJava;

with RxJava,

in REST implementation class, we had API call methods. I shared one of them. After started using RxJava, we need to change this method implementations. We changed method return types to Observable. After doing neccessary changes, method looks like that.

We are done with REST implementation class. After this, We need to change usecase implementation class and methods.

right now, our method getRadioList return an Observable stream data to our UI. As you can see, we don’ t need to send our data through an event bus. You can also filter, combine, cache, or manipulate this stream data depending on your need.

What we have learned

Although using RxJava is not so easy to use, replacing Otto with RxJava has removed many boilerplate callback code block from our codebase. In my opinion, RxJava is the best option for getting asynchronous data requests from any REST API.

if you have better ways to do it, feel free to comment here, or share an example repo.

mobiwise blog

Mobile Wisdom

Murat Can Bur

Written by

Blogger, Android Developer

mobiwise blog

Mobile Wisdom