RxJava in Android

Many people already using RxJava in their application for reactive programming.The context of this blog is the little bit test of What RxJava, and also show you real-life examples.It’s impossible to tell you everything about RxJava in this one blog.

Typical Code

In above code, User ‘s data come from the network or database.Something that you can’t really synchronously call to because of the block main thread.In that case, you can resort to a callback interface, but the callback interface has a couple of problems:

  1. It’s hard to control lifecycle.
  2. You can’t generally cancel it.
  3. It’s hard to change multiple callbacks.(if you have to do multiple things you have to coordinate all these callbacks)
  4. There’s no real standard way to do an error handling.
  5. Then there is also the question of what thread is this callback returning? You really want to return the original to the main thread usually.

AsyncTask is sort of solution to that is still not great because there is still the problem of changing and there’s still the problem of error handling.Error handling with AsyncTask is kind of ugly.

Let’s turn it to reactive. We want the user to become a stream of users. So, anytime users updated it’s just going to notify us that there’s a new value.

When we apply RxJava to this problem, we create an observable that returns us, users. Once you invoke this method, nothing is actually executed. It’s setting up a stream where once you subscribe to it, it will start giving you these user objects once you execute, or subscribe to this code.

An observable also has built-in error handling. You can cancel it, it can be either synchronous or asynchronous.Error handling is part of the stream and it propagates through the stream automatically there’s specialize call.Whenever data changed it gets pushed into you.You never have to pull it out.

Unsubscribe

If you are done with a stream, you want to unsubscribe. For example, if you have a fragment or an activity, you’re dealing with lifecycle, you want to cancel the subscription. You want to cancel the observable emitting events. If you don’t unsubscribe, you might leak memory.

RecyclerView Pagination using Rxjava

A common application feature is to load automatically more items as the user scrolls through the items. This is done by triggering a request for more data once the user crosses a threshold of remaining items before they’ve hit the end.

Define a dependency to RxJava 2.0 and retrofit

Our application to access the network we need to declare the INTERNET permission in the Android manifest file.

We have the following User model class:

Observing Users

Observe the task’s result or outcome on the main thread. You would declare you’re Flowable to be observed on the main thread.

Now, to use this interface during runtime, we’ll need to build a Retrofit object

CompositeDisposable is a disposable container that can hold onto multiple other disposables and offers add and removal disposable.

PublishProcessor

PublishProcessor can’t be instantiated through new but must be created via the create() method. Example usage.

Flowable

The difference from Observable is that the function of Backpressure is available.

Flowable creates a user stream that informs data every time it is subscribed.

concatMap

you take item emission and turn them into observables that get folded back into the stream.

RecyclerView Pagination

A common application feature automatically loads more items as the user scrolls through the items (infinite scroll). This is done by triggering a request for more data once the user crosses a threshold of remaining items before they’ve hit the end.

RecyclerView Pagnation

 

It’s important to fetch data before the user gets to the end of the list. Adding a threshold value. therefore, helps anticipate the need to append more data.

Implementing with RecycleView

Every Adapter  has support for binding to the OnScrollListener events which are triggered whenever user scrolls. Using this, we can define a basic UserPagnation which supports most use cases by creating our own class that extends OnScrollListener.

This is an abstract class, and that in order to use this, you must extend this base class and define the onLoadMoremethod to actually retrieve the new data. For example:

 

Download this project from GitHub.