Nowadays everybody is talking about Reactive Programming and you’re curious in learning this new thing called Reactive Programming. Maybe you’ve seen it used a few places but you’re still a little confused and would like some clarifications.
In this article, we are going to learn the basic concepts of the Reactive Programming. Starting from the next article we are going to do some real programming and learn how to use RxJava in the Android application development.
So, first let’s understand what are the problem we are facing? Why do we require Reactive Programming? Because if there is no problem, then we don’t need a solution right??
Why do we need Asynchronous work?
The simple answer is we want to improve the user experience. We want to make our application more responsive. We want to deliver a smooth user experience to our users without freezing the main thread, slowing them down and we don’t want to provide the jenky performance to our users.
To keep the main thread free we need to do a lot of heavy and time-consuming work we want to do in the background. We also want to do heavy work and complex calculations on our servers as mobile devices are not very powerful to do the heavy lifting. So we need asynchronous work for network operations.
The evaluation matrix:
Let’s see what do we need from the library that handles all the asynchronous work. You can imagine below 4 points as the evaluation matrix for the asynchronous library.
- Explicit execution: If we start the execution of a bunch of work on a new thread, we should be able to control it. If you are going to perform some background task, you gather the information and prepare them. As soon as you are ready, you can kick-off the background task.
- Easy thread management: In asynchronous work, thread management is the key. We often need to update the UI on the main thread from the background thread in the middle of the task or at the end of the task. For that, we need to pass our work from one thread (background thread) to another thread (here main thread). So you should be able to switch the thread easily and pass the work to another thread when needed.
- Easily composable: Ideally, It would be great if we can create an asynchronous work and as we start spinning background thread, it just do it’s work without depending any other thread (especially on UI thread) and stays independent from the other thread until it finishes its job. But in the real world, we need to update the UI, make database changes and many more things that make threading interdependent. So the asynchronous library should be easily composable and provide less room for the error.
- Minimum the side effects: While working with multiple threads, the other thread should experience minimum side effects from the other thread. That makes your code easily readable and understandable to a new person and it also makes error easily traceable.
What is Reactive Programming?
According to wikipedia:
Reactive programming is a programming paradigm oriented around data flows and the propagation of change. This means that it should be possible to express static or dynamic data flows with ease in the programming languages used, and that the underlying execution model will automatically propagate changes through the data flow.
In simple words, In Rx programming data flows emitted by one component and the underlying structure provided by the Rx libraries will propagate those changes to another component those are registered to receive those data changes. Long story short: Rx is made up of three key points.
RX = OBSERVABLE + OBSERVER + SCHEDULERS
We are going to discuss these points in detail one by one.
- Observable: Observable are nothing but the data streams. Observable packs the data that can be passed around from one thread to another thread. They basically emit the data periodically or only once in their life cycle based on their configurations. There are various operators that can help observer to emit some specific data based on certain events, but we will look into them in upcoming parts. For now, you can think observables as suppliers. They process and supply the data to other components.
- Observers: Observers consumes the data stream emitted by the observable. Observers subscribe to the observable using subscribeOn() method to receive the data emitted by the observable. Whenever the observable emits the data all the registered observer receives the data in onNext() callback. Here they can perform various operations like parsing the JSON response or updating the UI. If there is an error thrown from observable, the observer will receive it in onError().
- Schedulers: Remember that Rx is for asynchronous programming and we need a thread management. There is where schedules come into the picture. Schedulers are the component in Rx that tells observable and observers, on which thread they should run. You can use observeOn() method to tell observers, on which thread you should observe. Also, you can use scheduleOn() to tell the observable, on which thread you should run. There are main default threads are provided in RxJava like Schedulers.newThread() will create new background that. Schedulers.io() will execute the code on IO thread.
3 simple steps to use Rx in your application
Let’s look into the basic example. This will explain 3 simple steps to use Reactive programming in your application.
Step-1 Create observable that emits the data:
Here database is an observable which emits the data. In our case, it emits the strings. just() is an operator. Which basically emits the data provided in the argument one by one. (We are going to look into the operators in detail in our upcoming articles. So, don’t worry about them.)
Step -2 Create observer that consumes data:
In above code snippet observer is an observer that consumes the data emitted by the database observable. It processes the data received and also handles error inside it.
Step-3 Manage concurrency :
At the last step, we define our schedulers that manage the concurrency. subscribeOn(Schedulers.newThread()) tells database observable to run on background thread. observeOn(AndroidSchedulers.mainThread()) tells observer to run on the main thread. This is basic code for reactive programming.
So by now you should be able to understand, why we need reactive programming, why we need them and how we can implement them. In the upcoming articles, we are going to learn how to use RxJava and it’s operators in detail.
What next? (Part 2)
Now, visit the next part to start some programming example in RxJava and how to use RxJava in your Android/Java project.
Part 2: Code your next android app using RxJava
RxJava is new hot topic in the world of Android Development. The only problem is that it is very difficult to…