#1 ~ Intro with RxJava in Android

raditya gumay
2 min readOct 30, 2016

--

Hi, this is my first story in Medium. I hope your guys are enjoy with this story. So, lets move on!

Here, i am want to talk about fancy library in Android which called “RxJava” and belong with “RxAndroid”.

First, why you as Developers should consider using RxJava in you Android Project? RxJava have tons of advantages, they can replace you Massive native thread in Android, which contains tons of difference of kind Threads, like ThreadPoolExecutor, Thread, AsyncTask, Runnable, JobScheduler, etc. With RxJava, you just only consider type of Scheduler, which you can see in below image.

Obviously, RxJava have 5 difference type of Scheduler.

  1. immediate(): Creates and returns a Scheduler that executes work immediately on the current thread.
  2. trampoline(): Creates and returns a Scheduler that queues work on the current thread to be executed after the current work completes.
  3. newThread(): Creates and returns a Scheduler that creates a new Thread for each unit of work.
  4. computation(): Creates and returns a Scheduler intended for computational work. This can be used for event-loops, processing callbacks and other computational work. Do not perform IO-bound work on this scheduler. Use Schedulers.io() instead.
  5. io(): Creates and returns a Scheduler intended for IO-bound work. The implementation is backed by an Executor thread-pool that will grow as needed. This can be used for asynchronously performing blocking IO. Do not perform computational work on this scheduler. Use Schedulers.computation() instead.

You can use, each of Scheduler type for you Thread. For Example

public Observable<Foo> getFoo(int start, int limit) {
return service.getFoo(start, limit)
.subscribeOn(Schedulers.newThread())
.observeOn(AndroidSchedulers.mainThread());
}

And also, Complex Multiple Call.

public Observable<Foo1> getFoo1(int start, int limit) {
return service.getFoo1(start, limit)
.subscribeOn(Schedulers.newThread())
.observeOn(AndroidSchedulers.mainThread());
}

public Observable<Foo2> getFoo2(int start, int limit) {
return service.getFoo2(start, limit)
.subscribeOn(Schedulers.newThread())
.observeOn(AndroidSchedulers.mainThread());
}

public Observable<Foo3> getFoo3(int start, int limit) {
return service.getFoo3(start, limit)
.subscribeOn(Schedulers.newThread())
.observeOn(AndroidSchedulers.mainThread());
}

private final Observable<Bar> combined = Observable.zip(
getFoo1(), getFoo2(), getFoo3(),
new Func3<Foo1, Foo2, Foo3, Bar>() {
@Override
public Bar call(Foo1 obj1, Foo2 obj2, Foo3 obj3) {
return new Bar(obj1, obj2, obj3);
}
});

So, start using RxJava, you will see the difference!!
In Next post i will write about how to implement multiple call apis using RxJava.

Hope you enjoy with this article. for further info, just comment or email me at gumay[dot]raditya[at]gmail[dot]com.

@r_adiit | Github | Linkedin

--

--