Codementor Events

RxAndroid

Published Apr 03, 2018Last updated Sep 30, 2018
RxAndroid

RxAndroid is the JVM implementation of ReactiveX in android. Some basics to know about RxAndroid are :

  1. RxAndroid was developed by Netflix.
  2. RxAndroid is a very useful library for handling asynchronous tasks in android.
  3. There are mainly three concepts in RxAndroid :
    (i) Observables.
    (ii) Observers.
    (iii) Subscribers

reactive-eh-tell-me-more.jpg

Observables

Observables are the objects that emit data i.e. these are the source from which the data comes.
Observables are an instance of the Observable class.
Observables emit data only if there is atleast one observer that subscribes for the data.
The Observable class has many static methods called operators for creating observables and these operators are responsible for the behaviour of the observables.

Observer

Observer is an object that consumes data emitted by the observable.
An Observable can have any number of Observers an observable emits data if there it atleast one observer which has subscribed for the data.
Observers are the instance of the interface observer.The interface consists of mainly three methods :
1. onNext()
2. onCompleted()
3. onError()

How it Works?

Whenever the Observable emits a data it calls the onNext() method of the Observer object OR Each time when an observable emits data the onNext() method of the Observer is invoked.If the observable emits an error then it is calls the onError() method of the observer exactly once.After finishing the data emission the observable calls sthe onCompleted() method of the observer.

Flow Diagram

export.png
For an example :
Here is a demo application that demonstrates how this works :
The first step to start working with RxAndroid is to add dependencies to the gradle scripts for the library.

compile 'io.reactivex.rxjava2:rxandroid:2.0.1'
compile 'io.reactivex.rxjava2:rxjava:2.1.0'

We need to add the rxjava dependency also for effective working.
Next make the UI of the app.Here we are maeking a basic UI with a button and a textview. Button has an onClick method as subscribeNow() which we will be defining in the MainActivity.java.
Here is the code for the UI :

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.example.mohitkumar.rxandroid.MainActivity">

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/text1"
        android:textAppearance="?android:textAppearanceLarge"
        />
    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Button"
        android:onClick="subscribeNow"
        android:id="@+id/butt1"
        android:layout_alignParentBottom="true"
        android:layout_marginBottom="30dp"
        android:layout_marginLeft="30dp"
        android:layout_marginRight="30dp"
        />
</RelativeLayout>

Next, in MainActivity.java file declare an Observable that emits String value and similarly declare an Observer that accepts String value.Also we declare the textview here :

private TextView textView;
private io.reactivex.Observable<String> mObservable;
private Observer<String> mObserver;

Below is the code where the Observable object emits the data.

mObservable = io.reactivex.Observable.just("RxAndroid is Amazing !!!!");

Now, we initialise the observer object so that we have an observer that subscribes to the observable object so that it can accept the data.On initialising an obsercer the code looks as below :

mObserver = new Observer<String>() {
           @Override
           public void onSubscribe(@NonNull Disposable d) {

           }

           @Override
           public void onNext(@NonNull String s) {
            
           }

           @Override
           public void onError(@NonNull Throwable e) {

           }

           @Override
           public void onComplete() {

           }
       };

Now we want that whenever we click on the button the textview displays the observable described text. So, we subscribe the observable to observer in the method subscribeNow() as follows :

 public void subscribeNow(View view) {

        mObservable.subscribe(mObserver);
    }

As soon as the observer subscribes to the observable we need to update the textview, therefore we go to the onNext() method where we get the receive data from the observable and we can use it as we want.Below is the code :

 @Override
            public void onNext(@NonNull String s) {
                textView.setText(s);
            }

Here is the link to this demo project :

https://github.com/batbrain7/TestRepo_2/tree/master/RxAndroid

Discover and read more posts from MOHIT KUMAR
get started