RxJava For Android - RxAndroid
Multithreading in Android has always been a huge task for engineers. RxJava has made life easy for all the Android developers using it. In this blog, we are going to discuss RxJava for Android specifically.
We are going to discuss,
- What is RxJava?
- What is RxAndroid?
- Use-Cases in Android
What is RxJava?
RxJava is a JVM library for doing asynchronous and executing event-based programs by using observable sequences. It's main building blocks are triple O's, Operator, Observer, and Observables. And using them we perform asynchronous tasks in our project. It makes multithreading very easy in our project. It helps us to decide on which thread we want to run the task.
To learn more about RxJava, click here.
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'Note: The reason being is there might be a chance that RxAndroid doesn't have the latest version of RxJava used in the project. So, using RxJava dependency we override the versioning of the internal RxJava version used in RxAndroid.
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 goto 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 do use multiple Asynctask 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.
Check many other use-cases solved using RxJava Operators here.
To see the implementation of RxJava click here.
Happy learning.
Team MindOrks :)