Android

Android RxJava Tutorial | Make your code reactive

Android RxJava Tutorial

In this tutorial, we will learn how to implement RxJava, Room and Retrofit 2.0 in Android Application Development.

RxJava:

It is one of the popular library that you should use in your android app.

RxJava is the ability to compose asynchronous events in a more straightforward way. To run multiple events in asynchronous way, we must use RxJava to make an app acts Reactive. (Reactive programming is programming with asynchronous data streams)

*** Observable & Observer:

In ReactiveX an observer subscribes to an Observable. Then that observer reacts to whatever item or sequence of items the Observable emits.

 

Room:

The Room persistence library provides an abstraction layer over SQLite to allow fluent database access while harnessing the full power of SQLite.

The library helps you create a cache of your app’s data on a device that’s running your app. This cache, which serves as your app’s single source of truth, allows users to view a consistent copy of key information within your app, regardless of whether users have an internet connection.

 

Retrofit 2.0:

Retrofit is a REST Client for Android and Java by Square. It makes it relatively easy to retrieve and upload JSON (or other structured data) via a REST based web service.

 

BASIC:

Observer observes the Observable. When an observable emits data, all of the observer of that observable will be notified immediately.

In this example, when app gets data from server, observer method called getObservableFetch() will be notified and will receive the data.

onSubscribe() will be called first.

onNext() will be called. In this method we do our necessary operations.

onError() will called, where there is an error occured.

onComplete() will be called when all of its task is finished.

 

Add these dependency in your app build.gradle

 

Here, we get data from our server using RxJava with Retrofit 2.0.  Then we filter those data using RxJava.

Filtered and non-filtered data both have been shown in RecyclerView.

We also save server data into our database using Room persistence Api.

— Without using call.enqueue, we are implementing our network call using this below code

 

MainActivity.java

 

RecyclerViewAdapterForWithoutFilterData.java

 

RecyclerViewAdapterForFilterData

 

Here I am not discussing about how to implement Room. See my previous tutorial to learn Room. It will help you to implement Room Api in your android app.

Click Here,

 

 

 

 

 

See how to implement Retrofit in your android App. There is a serious of tutorial for you.

  1. http://shaoniiuc.com/android/android-login-registration-form-using-retrofit-php-mysql/
  2. http://shaoniiuc.com/android/fetch-json-api-data-recyclerview-using-retrofit-library/
  3. http://shaoniiuc.com/android/image-upload-retrofit-library/

 

Note: Retrofit 2 also supports RxJava extensions. You will need to create an RxJava Adapter. By default, all network calls are synchronous:

 

Without using Call invocation, we are using Observable pattern to observe on this call. (RxJava)

 

 

 

 

Leave a Reply

Your email address will not be published.

fifteen − ten =