RxAndroid
RxAndroid is an extension to RxJava. It providers a scheduler to run code in the main thread of Android. It also provides the ability to create a scheduler that runs on an Android handler class. With this schedulers, you can define an observable which does its work in a background thread, and post our results to the main thread. This allows for example to replace a AsyncTask implementations that RxJava.
The Observer pattern done right. ReactiveX is a combination of the best ideas from the Observer pattern, the Iterator pattern, and functional programming.
To use RxJava in Android add the following dependency to your build.gradle file.
compile 'io.reactivex.rxjava2:rxandroid:2.0.1'
compile 'io.reactivex.rxjava2:rxjava:2.0.8'For example, you can define a long-running operation via the following observable.
final Observable<Integer> serverDownloadObservable = Observable.create(emitter -> {
SystemClock.sleep(1000); // simulate delay
emitter.onNext(5);
emitter.onComplete();
});You can now subscribe to this observable. This triggers its execution and provides the subscribe with the required information.
subscribeOn(Schedulers.io())Threading in Rxandroid is done with the help of schedulers. To be more precise schedulers are like a ThreadPool managing one or more Threads. So, If a schedulers need to perform a task then it will take a Thread from pool and will run that task on it.
Get Ronak Sethi’s stories in your inbox
Join Medium for free to get updates from this writer.
Subscribeon() specifies on which Scheduler the work will be done.In our case it is to make a network and Schedulers.io() is backed by unbounded ThreadPool. It can be used for non CPU-intensive I/O type like network calls and database calls etc.And if you want you can dig more about it and you really should.
For example, let's assume you assign this to a button.
serverDownloadObservable. observeOn(AndroidSchedulers.mainThread()).
subscribeOn(Schedulers.io()).
subscribe(integer -> {
updateTheUserInterface(integer); // this methods updates the ui
view.setEnabled(true); // enables it again
});
}
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread());The subscriber observes in the main thread while Observable is called outside the main thread.
As we are only interested in the final result, we could also use a Single.
Subscription subscription = Single.create(new Single.OnSubscribe() {
@Override
public void call(SingleSubscriber singleSubscriber) {
String result = doSomeLongRunningStuff();
singleSubscriber.onSuccess(value);
}
})
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(new Action1() {
@Override
public void call(String value) {
// onSuccess
updateTheUserInterface(); // this methods updates the ui
}
}, new Action1() {
@Override
public void call(Throwable throwable) {
// handle onError
}
});Unsubscribe to avoid memory leaks
Observable.subscribe() returns a Subscription (if you are using a Flowable) or a Disposable object. To prevent a possible (temporary) memory leak, unsubscribe from your observables in the ‘onStop()’ method of the activity or fragment. For example, for a Disposable object you could do the following:
@Override
protected void onDestroy() {
super.onDestroy();
if (bookSubscription != null && !bookSubscription.isDisposed()) {
bookSubscription.dispose();
}
}Thanks for reading!









