Start a Project

RxAndroid: From begining to some use cases

Android code architecture is always a hot topic in android application development. There is always some better architecture/pattern developed which help developers to write better apps.  One such approach is Reactive Programing or RX.

In Reactive programming we have two things of interest, the first is the Observable, the second is the Subscriber or the Observer. The Observable does all the work and the Subscriber is used to listen to the various states of the Observable, an Observable can either complete or fail; this will be reflected in the onComplete or the onError methods in the Subscriber, there is also a method called as onNext which will execute when an Observable emits an item. ~source

So in this blog, we will see how to add Rx in an Android project along with its caveats.

GETTING STARTED WITH RX ~ Adding RxAndroid in an existing project.

We are using  Retrofit2 for all our network calls. Here are the latest dependencies used to add RxAndroid in the project.

Here we are using different versions of RxAndroid and RxJava for bug fixes and new features added to RxJava library. Also, We are using Rx Java adapter in order to make the Retrofit2 client compatible with RxAndroid.

We can build our Retrofit client like in the sample given below. Please note we are using RxJava2CallAdapterFactory for compatibility with Retrofit2.

 

Rx: Observing on the main thread

By Default Rx is Synchronous in nature but we can update use it asynchronously without using tedious AsyncTask just by simply adding subscribeOn().

The Observable class has many static methods which create observable objects. The following code shows you how to use the just() operator to create a very simple Observable that emits a single String.

 

Rx: Sample example

Here is a sample request where we can use subscribe the Observable on available io thread and observer on the main thread.

 

 

Rx: Use cases

There are varieties of use cases where developers enjoy using Rx in apps. Rx can be used with kotlin or java8 which will remove redundant boilerplate code. Rx solves many concurrency and multi-threading problems that we face in the application such as combining API calls (fetching results from different API call simultaneously and combine them), multiple data sources (like one from API source x and another from API source y), event click handling, efficient search suggestion and like that.

Let us take an example of combining API calls using zip operator:

In this example, there are two observable create observableOne and observableTwo on IO thread so that network operation can be performed on it efficiently without creating a new thread. We are using static operator zip on both observable for combining results.

 

As you can see in above logs both request are fired and we don’t need redundant check whether a request has been completed or not.

 

Unsubscribing Observable

If we want to make the application more efficient we need to properly unsubscribe the observable it whenever the activity/fragment is destroyed. For this, we use the CompositeDisposable class which is a disposable container that can hold onto multiple other disposables and * offers O(1) add and removal complexity.

 

That is each request is added to the CompositeDisposable list and clear whenever the activity/fragment is destroyed or whenever some condition is met. Here is a ready baked code for you.

 

Using CustomObserver

We can use custom observer instead or Observer provided by reactiveX libraries. The main advance of using it is to promote OOPs in architecture.

 

Few notes:

Exit mobile version