Android App Development
iOS App Development
Flutter App Development
Cross Platform App Development
Hire on-demand project developers and turn your idea into working reality.
Big thanks to Webkul and his team for helping get Opencart 3.0.3.7 release ready!
Deniel Kerr
Founder. Opencart
Top Partners
Updated 6 March 2021
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.
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.
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.
Here is a sample request where we can use subscribe the Observable on available io thread and observer on the main thread.
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.
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.
We can use custom observer instead or Observer provided by reactiveX libraries. The main advance of using it is to promote OOPs in architecture.
To detach an observer from its observable while the observable is still emitting data, we can call the unsubscribe() method on the Subscription object. e.g. mySubscription.unsubscribe();
Using the subscribeOn and observeOn operators, we can explicitly specify which thread should run the background job and which thread should handle the user interface updates.
https://medium.com/@arifnadeem7/step-by-step-android-app-development-using-the-reactive-programming-paradigm-rxjava-aa1c63a66650
https://www.toptal.com/android/functional-reactive-android-rxjava
https://medium.freecodecamp.com/rxandroid-and-kotlin-part-1-f0382dc26ed8
https://github.com/kaushikgopal/RxJava-Android-Samples
That’s all it take for lazy programming. Write efficient n clean code.
Your email address will not be published. Required fields are marked*
Name*
Email*
Save my name email and website in this browser for the next time I comment.
We use cookies to personalize your experience. By continuing to visit this website you agree to our use of cookies. Learn more about privacy policy
Name
Email
Subject
Enquiry or Requirement
If you have more details or questions, you can reply to the received confirmation email.