Converting RxJava 1.0 Observables

We will quickly see that it is super easy to convert Observables from the old interface to the new one.

First of all, let's create an old-typed Observable that we can play with, something like this:

rx.Observable.just("One", "Two", "Three")
.doOnNext(i -> log("doOnNext", i))
.subscribe(i -> log("subscribe", i));

To convert that to a new Observable of the io.reactivex.Observable type, we will need to use RxJavaInterop.toV2Observabe():

RxJavaInterop.toV2Observable(rx.Observable.just("One", "Two", "Three"))
.doOnNext(i -> log("doOnNext", i))
.subscribe(i -> log("subscribe", i));

It worth noting that it is especially useful to use the static import in such cases so that the code can be simplified to the following:

import static hu.akarnokd.rxjava.interop.RxJavaInterop.*;

toV2Observable
(rx.Observable.just("One", "Two", "Three"))
.doOnNext(i -> log("doOnNext", i))
.subscribe(i -> log("subscribe", i));

Actually, if the toV2Observable call is used very often, it might be worth to write your own wrapper to make it less intrusive:

static <T> Observable<T> v2(rx.Observable<T> source) {
return toV2Observable(source);
}

So, the code will now look this:

v2(rx.Observable.just("One", "Two", "Three"))
.doOnNext(i -> log("doOnNext", i))
.subscribe(i -> log("subscribe", i));

However, that depends on a personal taste and the guidelines that your team prefers. It might not be a terribly useful tip, but it's important away from introducing your own abstractions in the code that make more sense in the context of the domain that you are working in.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.117.137.12