Tagbangers Blog

Async task with RxJava in android

Hi there! I'm Sylvester and I've been working here like 6 months I guess? So I've been working on some projects and one of the projects is app development for android. It was my first time working on an app, of course. I came across a few terms during the project and one of them is "async task". So for this project, I decided to use RxJava to handle async tasks, and yes, I would like to share what I learned from the project!

...

So what is an async task?

I guess you can say that it's a heavy, time-consuming task that we can run in the background while doing other stuff on the UI thread.

Ok, let's jump to RxJava. There are 3 components that we will need for reactive programming, which is

Observable - Class that emits data
Scheduler - For thread management
Observer - Consumes the data emitted by observable, and perform various operation on the callback


First, let's add the dependency

We'll also be needing RxAndroid, a Reactive Extensions for Android which provides a Scheduler that schedules on the main thread or any Looper

Gradle Project

implementation 'io.reactivex.rxjava2:rxjava:2.x.x'
implementation 'io.reactivex.rxjava2:rxandroid:2.0.1'

Maven Project

<dependency>
<groupId>io.reactivex.rxjava2</groupId>
<artifactId>rxjava</artifactId>
<version>2.x.x</version>
<groupId>io.reactivex.rxjava2</groupId>
    <artifactId>rxandroid</artifactId>
    <version>2.0.0-RC1</version>
</dependency>

...


Observable

Here's some of the method to create an observable

@GET("foo/bar")
Observable<T> getRequest(@HeaderMap Map<String, String> parameters);
// or
Observable<String> observable = Observable.just("Hello World");
// or
Observable<String> observable = Observable.fromArray("a", "b", "c");
// or
Observable<String[]> observable = Observable.just(new String[]{"1", "2", "3", "4"});
// or
Observable.create( foo -> {
// some function that will emit data
});
//and more


Scheduler

observeOn() - method to tell observers on which thread to observe.

scheduleOn() - method to tell the observable on which thread it should run.


Observer

Consumes the data and performs some operation on the callbacks

observable
        .subscribeOn(Schedulers.io())
        .observeOn(AndroidSchedulers.mainThread())
        .subscribe(
                new Observer<String>() {
                    @Override
                    public void onSubscribe(Disposable d) {
                        // do something
                    }

                    @Override
                    public void onNext(String s) {
                        // do something
                    }

                    @Override
                    public void onError(Throwable e) {
                        // do something
                    }

                    @Override
                    public void onComplete() {
                        // do something
                    }
                });


The observer can also be written as below, depending on which callbacks that we need

observable
        .subscribeOn(Schedulers.io())
        .observeOn(AndroidSchedulers.mainThread())
        .doOnSubscribe(/* do something */)
        .doOnNext(/* do something */)
        .doOnError(/* do something */)
        .doOnComplete(/* do something */)
        .subscribe();


...


So that's one of the ways on how to handle an async task with RxJava. I hope it'll be useful to someone! See ya!