Networking with RxJava and Live data
Why RxJava?
In Android, We should perform all long task in background and update the result in Main thread so that the process doesn’t freeze the UI. Typically such process are handled performed by AsyncTask
in Android. But, Hey we are too lazy to maintain the Threads and Handlers. Here’s where RxJava pops up.
RxJava takes care of threading, synchronization and thread-safety.
Here is an example
Observable.just("one", "two", "three", "four", "five")
.subscribeOn(Schedulers.newThread())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(/* an Observer */)
This will execute the
Observable
on a new thread, and emit results throughonNext
on the main thread. ( Yeah! I copied above snippet and caption🤪)
By using RxJava we can make Networking in Android easy.
Most commonly used Operators?
Before diving into Networking with Android, Let us get into get into basic operators that need to make a API call in Android with RxJava
- Observable
- Flowable
- Single
Observable : Let us consider that We are making an API call and receiving the response. And that response is wrapped inside Observable type so that it can be processed by RxJava operators.
Flowable : Each operator solves different problem, Flowable solving Backpressure ( Backpressure happens when there are multiple responses come at a speed that observers cannot keep up ). In Flowable, BackPressure is handled using BackPressure Strategies — MISSING, ERROR, BUFFER, DROP, LATEST. BackPressure is Handled by anyone of the mentioned strategies. Flowable is useful when we are making pagination call.
Single : Single always either emits one value or an error. It returns Latest response for all requests. It is ideal for making search call.
How to make API calls using RxJava?
For this example we are going to use Retrofit + Gson + RxJava + RxAndroid
Dependencies
Basic Retrofit setup
Interface for API calls
Stitch everything together
We have made basic setup Let us see, How we can make API call using this. We are going to use Flowable for this example.
Where Live data comes in?
But, Why live data? If you look at the live data, it handles the life cycle for us. In other hand RxJava gives us more power to manipulate the data that we get from the response like filter, map, flat map. So, why not combine best of both world and get some cool things out of it? To achieve this we are going to use LiveDataReactiveStreams, it basically converts Publisher’s Reactive Streams into LiveData. LiveDataReactiveStreams will take care of the Disposing the observers and subscriptions for RxJava.
The above mentioned example should work fine for most of all the process, but when there is no internet we have to handle Error by our self. Let’s consider that you have a “Activity-A” and “Activity-B”. You make a API call in “Activity-A” and move to “Activity-B” unfortunately internet connection is lost when you come back from “Activity-B” to “Activity-A”, Even if you didn’t trigger the API call on coming to the “Activity-A”. API call will be made.
But why API is call triggered here? If you notice that we are returning Livedata from makeAPICall(), Live data lifecycle aware component, when ever that UI comes into scope new Value from Life data is fetched, Hence the API call is made. To overcome this we will set and remove source making that live data will emit the data only once.
Reference :
- https://github.com/ReactiveX/RxJava
- https://www.boredapi.com/documentation : API that is used for in this article