RxAndroid Tutorial Intro:
Reactive extensions usage is the current hottest trend in most programming languages like Java, JavaScript, Swift etc. Rx Android is the reactive extension for Android which is built on top of RxJava with minimum classes addition to it which makes it easier to add for Android Projects. This RxAndroid Tutorial is to help understand the usage of Reactive extensions in our Android Projects.
In this Introductory tutorial, we will be discussing about basic terminology and Addition of RXAndroid Framework library to our Android Project. So let us get started.
This article is part of RxJava Intro series. You can checkout the entire series here:
- RxAndroid Introduction – RxJava Tutorial #1 – You are Here
- RxAndroid Example – RxJava Tutorial #2
- RxJava Operators In General – RxJava Tutorial #3
- RxJava Operators for Creating Observables – RxJava Tutorial #4
- RxJava Operators for Transforming Observables – RxJava Tutorial #5
- RxJava Operators for Filtering Observables – RxJava Tutorial #6 – Coming Up
- RxJava Operators for Combining Observables – RxJava Tutorial #7 – Coming Up
What is RxAndroid
RxAndroid is a framework to utilize the reactive extensions for Android Projects which is built on top of RxJava. RxAndroid is used to handle Asynchronous tasking efficiently in Android Applications.
What are Reactive Extensions (Rx)
Reactive Extensions (Rx) is a library for performing asynchronous programming with observable streams.
Data sequences exists in different forms such as stream of data from a file (or) user input (or) push notifications (or) web service requests etc.
Reactive Extensions represent all these data sequences as observable sequences which emits the data. Now an application can subscribe to these observable sequences to receive the new data asynchronously.
Related Articles:
Using Java 8 Features in Android – Android Java 8 Tutorial #1
Why Reactive Programming / RxJava?
With the increase of the Quad core, Octa core processors into Android World, our app would be butter smooth if we can harness the power of MultiThreading.
Android provides numerous concepts for achieving MultiThreading such as AsyncTask, Thread class, Runnable interface, ThreadPoolExecutor, etc., but extensive usage of these results in complex, error prone code.
RxJava and RxAndroid provides special Schedulers and operators to ease the conversion of this complex multithreading task into an easy way of specifying which thread your work needs to be done and which thread the results need to be updated. This concept is very much useful for android as we all know that we need to do tasks such as Networking etc. in background threads and our App’s UI updation related tasks in our Android’s Main thread.
RxJava2.0 is shipped with many new Schedulers like Scheduler.newThread() which creates a new thread to perform some work.
Basic Building Blocks for Rx Programming
There are 2 main building blocks for Reactive / Rx Programming
1. Observable:
Observables simply emits the data to those which subscribed to them. All the emission is done asynchronously to the subscribers.
Example code:
// RxAndroid Tutorial - Adding Observable Observable<String> stringObservable = Observable.just("Hello Reactive Programming!");
2. Observer
Observer consumes the data emitted by the Observable. To do this, Observer needs to subscribe to the Observable.
// RxAndroid Tutorial - Adding observer Observer<String> stringObserver = new Observer<String>() { @Override public void onSubscribe(Disposable d) { } @Override public void onNext(String s) { Toast.makeText(MainActivity.this, s, Toast.LENGTH_SHORT).show(); } @Override public void onError(Throwable e) { } @Override public void onComplete() { } };
3. Subscribers
Subscriber helps an Observer subscribe /unsubscribe from Observable.
// RxAndroid tutorial - observer subscribing to observable stringObservable.subscribe(stringObserver);
Note:
An Observable emits data if there is at least one Observer subscribed to it.
Adding RxAndroid to our Android Project
Now we will add RxAndroid Library to our Android Studio Project. We’ll open our app module’s build.gradle file and add the following line of code to include RxAndroid to our Project
compile 'io.reactivex.rxjava2:rxandroid:2.0.1'
However since RxAndroid’s releases are not as frequent as RxJava’s ones, we will add RxJava’s Library to our project in order to not miss the bug fixes and latest enhancements.
compile 'io.reactivex.rxjava2:rxjava:2.1.3'
In the next article, we will dig deeper along with an example code in Android Studio.