(Part 1 of 3) Networking with RxJava on Android Like A Boss

Rizki Maulana
3 min readNov 10, 2019

--

As a developer that working on client side, sometimes we have to face some problem related with asynchronous task. Networking is one of biggest task that we have to handle asynchronously

Source : https://miro.medium.com/

For example we have to do three network calls simultaneously and we have to make sure all call is successfully delivered by client or we have to make two network call that one call is depending to another call to success and many more challenges related to asynchronous task

In this article we will talk about how to take advantage of RxJava to handle our daily problem as Android Developer to handle asynchronous on Networking. As a networking library we well use Retrofit (but we can use any networking library that support reactive programming, for example RxFastAndroidNetworking). All we have to do with our existing project (that use retrofit) is add RxJava2CallAdapterFactory, find out here : https://github.com/square/retrofit/tree/master/retrofit-adapters/rxjava2

I assume that we already know about basic RxJava and RxAndroid, here is good article about it :

In this article we will focus about how to take advantages of RxJava + Retrofit to handle networking with several problem state that we usually face as Android Developer

Scenario :

We have to make three http call at same time, make sure all call is success and handling every success call

For example we have three calls that have return api as List<ContactDto>, UserDto, ProductDetailDto. All those dto has BaseResponse class like this :

Problem State :

  1. How we can handle every success call with different action. For example, first call to update list, second call to get detail user, third call to get detail product)
  2. How we can make sure if all call is already complete (with or without error). For example, determine to dismiss progress / loading dialog when all call is complete

Conventional Approach :

Create API Interface :

Create http call :

With this kind of approach, we will face several problem related asynchronous handling :

  1. We cannot determine when all call already complete to dismiss progress dialog. Because it can be happen when first call is complete (and progress dialog is dismissed) but second and third call is still ongoing, it means UI of product detail and user detail is not updated yet but progress dialog is dismissed, it will indicated that call is complete and user will suppose that product detail and user detail is empty
  2. When there is connection problem, for example there is no internet connection. It means all call will failed, when all call failed at same time it will show error dialog (if handling error using dialog) three times at same time too, even though we only need show error dialog once
  3. There is no listener that will tell us if all call is success, every single call has each success listener

RxJava Approach :

We will use RxJava Approach to solved those problems. We will use merge operator to handle this, although there is a lot of approach to handle this case.

Create API Interface :

Create http call :

With this RxJava Approach, we can solve those three problem when do three (or more) API call at same time.

  1. doFinally will guarantee that out code execute when all request already complete, with or without error
  2. When all call failed, it will only call onError once, then we can show error dialog properly to user
  3. onComplete guarantee that all call complete successfully

Actually we can do more with merge operator, this is only one of example. To solve this case we also can user flatMap operator or zip operator as long as we know how those operators work.

In second part we will learn how to add debounce to our http call to create more efectif call when user have to do rapid call to network, for example when user use autocomplete text that the data supplied by server.

Thank you

--

--