Network Chaining With RxJava

Fazal Hussain
Apr 29 · 3 min read
Image Credit

Mobile apps are more powerful than ever before. It supports all type of user interaction and connects multiple data sources, people are more demanding and expect more excellent applications and its the responsibility of an Android developer to meet the needs of the modern users to build the app which is more responsive, robust and easy to use. Reactive programming with Kotlin will help you to achieve that goal.

What is Reactive Programming?

Reactive is a declarative programming paradigm which concerns with a stream of data.

Reactive systems are responsive which mean that while navigating on different screens things are not logged up. The UI thread always remain responsive if there is any error it picked the error and notify to user and user will use the app continuously

The apps are not responsive. The user will be frustrated and you will lose the application users.

Reactive systems are message-driven. The underline mechanism for communication is asynchronous message passing system which is provided by reactive API. The message-driven system decouples the various component and makes it independent from one another

Let's take an example of simple user interaction. If the user clicks the simple button and there is no progress bar so without thinking anything user taps multiple times because users are impassioned and, users are more impassioned in term of interacting with application & software. If users tap multiple times it will open multiple activities, multiple network calls and, your data will be redundant.

Frequent taps on the button

To solve this problem we have multiple ways to do it

  1. Temporarily disable the button
  2. Write a custom view for the button.

But, these approaches create some boilerplate code to avoid this problem reactive programming come into play and make our code more precise.

helloBtn.clicks().throttleFirst(1000, TimeUnit.MILLISECONDS)
.subscribe {
//Do your work here
}

The above code snippet ignores the frequent clicks. It emits the click event after one second.

Button click emission

As you can see that the frequent clicks are ignored. It will emit the click event after some interval of time.

Chaining Network Calls

As you know that network calls run in a background thread. It is very hard to know how long a network call will take. This is the way the developer always look to handle network request asynchronously. Traditionally, network call handled asynchronously. For initiating multiple network request we had to nested async task which leads toward different problems

  1. Hard to handle errors.
  2. Memory Leaks
  3. Null Exceptions

Rx is well suited in this kind of problems. It is treated network calls as an asynchronous data stream which manipulate them and chain them together

Network call chaining using Reactive
eventService.
.getEventID
.subscribeOn(Schedulers.io())
.flatMap {
result -> eventService.getAllRelatedEvent(result)
}
.observeOn(AndroidSchedulers.mainThread())
.subscribe({
//handle all related events

}, {
//handle error

})

First, we get the event Id based on the result, we are calling another network call using a flat map operator and observe the result in the android main thread.

Subscribe method has two parameters, the first is a handler for success cases on the other hand second is the handler for error cases. In case of success, you will get the list of related events and you can call some method to handle it. In case of error, you get whatever error occurs during the network chaining its up to you how you handle the errors.

By using RxJava and RxAndroid, we can do network chaining with one block of code and handle error easily.

Javarevisited

An humble place to learn Java and Programming better.

Fazal Hussain

Written by

Android developer — Technical Writer

Javarevisited

An humble place to learn Java and Programming better.