Why Retrofit used with RxJava Part 1

Probably you have asked yourself this question before !

Retrofit Already in run on background thread. Then why need another background task RxJava?
I don’t know what is exact benefits of Retrofit + RxJava = ?
I searched more than one month no answer still now.. Anyone answer for future users including me :)

I found this interesting question in SOF and i decided to write my thoughts and share it with you.

From the community and from what i know about Retrofit + RxJava here are some points that may clear up the idea for you.

1- I think most importantly, avoid nested callbacks(callback hell)

callbacks method are awful it makes you code looks ugly and hard to maintain and trace, therefore you need to get rid of callbacks whenever you can.

2- Make the best use of RxJava operators like (map, flatmap, zip…etc)

For better architecture and minimum code you have to benefit from using RxJava operators to manipulate your data and make your business layer completely separated from other layers.

3- RxJava make it easier for you to switch between design patterns like MVP and MVVP design pattern.

in this case i’m talking about android, when you move to MVVM design pattern you get rid of the presenter layer as you switch to the concept of whoever is interested subscribes to the needed data and our VM will not be interested to know who is going to consume this data.

4- RxJava is a Java VM implementation of ReactiveX (Reactive Extensions) a library for composing asynchronous and event-based programs by using observable sequence

Whats interesting about this that an observable ends up with either call onNext then onComplete methods if things works fine, or onError is something happened, a very handy and amazing way for error handling so not big worries about errors you just need to handle different types of errors.

5- It makes data flow like a stream, chaining of operations to avoid nested callbacks

RxJava will give you the experience of how to work with streams and chaining you data streams with a good error handling mechanism and data manipulation. RxJava is not just for background tasks its a complete pattern, and if you use it in one layer you are not benefiting the most of it, which is transferring all you application into streams throw observables, streams can be used as an inputs to other streams (functional part. Adding to this one of the most important operators in RxJava is zip operator which allow you easily to combine multiple calls into one.

One big challenge when using RxJava + Retrofit 2 !

please continue to Part 2

Hopefully this article will help you to add more thoughts about this question. please if you liked this article press ❤ button and follow me for more articles.