Concat

.concat() can be used to merge two Observables, as in the following example:

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

This will yield the given output:

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

This is not very surprising at all. However, it will be a different case if we execute code like this:

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

.concat() waits for the first Observable to complete before it starts taking values from the second Observable. Since .interval() never completes, the values from the following will never be printed:

Observable.just(-1L, -2L)

This will be the only output:

APP: subscribe:RxComputationThreadPool-1:0
APP: subscribe:RxComputationThreadPool-1:1
APP: subscribe:RxComputationThreadPool-1:2
APP: subscribe:RxComputationThreadPool-1:3
...

However, if we change the order of the Observables to this:

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

The values of the first Observable will be printed and also, the second Observable will get a chance to produce output but, again, it will never complete:

APP: subscribe:main:-1
APP: subscribe:main:-2
APP: subscribe:RxComputationThreadPool-1:0
APP: subscribe:RxComputationThreadPool-1:1
APP: subscribe:RxComputationThreadPool-1:2
APP: subscribe:RxComputationThreadPool-1:3
..................Content has been hidden....................

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