rx android tutorial

RxAndroid Tutorial #1 – Getting started with Reactive Extensions in Android

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.

rxandroid tutorial

In this Introductory tutorial, we will be discussing about basic terminology and Addition of RXAndroid Framework library to our Android Project. So lets get started.

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:

2. Observer

Observer consumes the data emitted by the Observable. To do this, Observer needs to subscribe to the Observable.

3. Subscribers

Subscriber helps an Observer subscribe /unsubscribe from Observable.

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

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.

In the next article, we will dig deeper along with an example code in Android Studio.

 

Related Posts