O'Reilly logo

Reactive Android Programming by Tadas Subonis

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Merge

Compared to .concat(), .merge() doesn't need to wait for the first Observable to complete before emitting values from the second one.

Let's take a look at this example:

Observable.merge(        Observable.just(1L, 2L),        Observable.just(3L, 4L))        .subscribe(v -> log("subscribe", v));

It will produce the same output as .concat():

APP: subscribe:main:1APP: subscribe:main:2APP: subscribe:main:3APP: subscribe:main:4

However, consider that we use the .interval() Observable as the first Observable, as illustrated:

Observable.merge(        Observable.interval(3, TimeUnit.SECONDS),        Observable.just(-1L, -2L))        .subscribe(v -> log("subscribe", v));

The Subscription will then produce values from both the Observables just fine:

APP: subscribe:main:-1APP: ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required