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:1
APP: subscribe:main:2
APP: subscribe:main:3
APP: 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:-1
APP: subscribe:main:-2
APP: subscribe:RxComputationThreadPool-1:0
APP: subscribe:RxComputationThreadPool-1:1

This is because .merge() doesn't wait for the first Observable to complete before emitting more values.

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

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