Thursday 18 January 2018

Operator - Concat



emit the emissions from two or more Observables without interleaving them




The Concat operator concatenates the output of multiple Observables so that they act like a single Observable, with all of the items emitted by the first Observable being emitted before any of the items emitted by the second Observable (and so forth, if there are more than two).

Concat waits to subscribe to each additional Observable that you pass to it until the previous Observable completes. Note that because of this, if you try to concatenate a “hot” Observable, that is, one that begins emitting items immediately and before it is subscribed to, Concat will not see, and therefore will not emit, any items that Observable emits before all previous Observables complete and Concat subscribes to the “hot” Observable.
In some ReactiveX implementations there is also a ConcatMap operator (a.k.a. concat_allconcat_map,concatMapObserverforforIn/for_inmapcatselectConcat, or selectConcatObserver) that transforms the items emitted by a source Observable into corresponding Observables and then concatenates the items emitted by each of these Observables in the order in which they are observed and transformed.
The StartWith operator is similar to Concat, but prepends, rather than appends, items or emissions of items to those emitted by a source Observable.
The Merge operator is also similar. It combines the emissions of two or more Observables, but may interleave them, whereas Concat never interleaves the emissions from multiple Observables.


Code - 

-------------------------------------------------------------------

Observable<Integer> seq1 = Observable.range(0, 3);
Observable<Integer> seq2 = Observable.range(10, 3);

Observable.concat(seq1, seq2)
.subscribe(System.out::println);

-------------------------------------------------------------------

concatWith
------------------------------------------------------------------

 Observable<Integer> seq1 = Observable.range(0, 3);
 Observable<Integer> seq2 = Observable.range(10, 3);
 Observable<Integer> seq3 = Observable.just(20);

 seq1.concatWith(seq2)
 .concatWith(seq3)
 .subscribe(System.out::println);

------------------------------------------------------------------














22

No comments:

Post a Comment

Calling method Sequencely

1. Execute multiple task sequencly (WAY-1) ------------------------------------------------------------------------------ import io.re...