A Short Note – RxJava And RxAndroid For Android

Hello Readers, CoolMonkTechie heartily welcomes you in A Short Note Series.

Multi-threading in Android has always been an enormous task for engineers. RxJava has made life easy for all the Android developers using it. In this note series, we are going to discuss RxJava for Android specifically.


So Let’s begin.


What is RxJava?

RxJava is a JVM library for doing asynchronous and executing event-based programs by using observable sequences. Its main building blocks are triple O’s, Operator, Observer, and Observables. And using them we perform asynchronous tasks in our project. It makes multi-threading very easy in our project. It helps us to decide on which thread we want to run the task.

But RxJava is made for primarily any Java projects. To use RxJava in Android, we will also need RxAndroid.


What is RxAndroid?

RxAndroid is an extension of RxJava for Android, which is used only in Android application.

RxAndroid introduced the Main Thread required for Android.

To work with the multithreading in Android, we will need the Looper and Handler for Main Thread execution.

RxAndroid provides AndroidSchedulers.mainThread() which returns a scheduler and that helps in performing the task on the main UI thread that is mainly used in the Android project. So, here AndroidSchedulers.mainThread() is used to provide us access to the main thread of the application to perform actions like updating the UI.

In Android, updating UI from background thread is technically not possible, so using AndroidSchedulers.mainThread() we can update anything on the main thread. Internally it utilizes the concept of Handler and Looper to perform the action on the main thread.

RxAndroid uses RxJava internally and compiles it. But while using RxAndroid in our project we still add the dependency of RxJava to work with like,

implementation 'io.reactivex.rxjava3:rxjava:3.0.0'
implementation 'io.reactivex.rxjava3:rxandroid:3.0.0'


Use-Cases in Android

RxJava has the power of operators and as the saying goes by, “RxJava has an operator for almost everything“.


Case 1:

Consider an example, where we want to do an API call and save it to some storage/file. It would be a long-running task and doing a long-running task on the main thread might lead to unexpected behavior like App Not Responding.

So, to do the above-mentioned task, we might think to use AsyncTask as our go-to solution. But with Android R, AsyncTask is going to be deprecated, and then libraries like RxJava will be the solution for it.

Using RxJava over AsyncTask helps us to write less code. It provides better management of the code, as using AsyncTask might make the code lengthy and hard to manage.


Case 2:

Consider a use-case where we might want to fetch user details from an API and from the user’s ID which we got from the previous API we will call another API and fetch the user’s friend list.

Doing it using AsyncTask we might have to use multiple Async task and manage the results in was way where we want to combine all the AsyncTask to return the result as a single response.

But using RxJava, we can use the power of zip operator to combine the result of multiple different API calls and return a single response.


Case 3:

Consider an example of doing an API call and getting a list of users, and from that, we want only the data which matches the given current condition.

A general approach is to do the API call, and from the Collection, we can then filter the content of that specific user based on the condition and then return the data.

But using RxJava we can directly filter out the data while returning the API response by using the filter operator, and we do all of this by doing the thread management.

These are a few use cases to understand RxJava for Android and why we need RxAndroid in our project.


Conclusion

In this note series, we understood about the RxJava and RxAndroid for Android. We discussed a few use cases to understand RxJava for Android and why we need RxAndroid in our project.

Thanks for reading ! I hope you enjoyed and learned about the RxJava and RxAndroid Concepts for Android. Reading is one thing, but the only way to master it is to do it yourself.

Please follow and subscribe us on this blog and support us in any way possible. Also like and share the article with others for spread valuable knowledge.

You can find Other articles of CoolmonkTechie as below link :

You can also follow official website and tutorials of Android as below links :

If you have any comments, questions, or think I missed something, feel free to leave them below in the comment box.

Thanks again Reading. HAPPY READING !!???

Loading

Summary
A Short Note – RxJava And RxAndroid For Android
Article Name
A Short Note – RxJava And RxAndroid For Android
Description
Multi-threading in Android has always been an enormous task for engineers. RxJava has made life easy for all the Android developers using it. In this note series, we are going to discuss RxJava for Android specifically.
Author

Leave a Comment