June 2017
Intermediate to advanced
400 pages
8h 44m
English
Similar to defaultIfEmpty(), switchIfEmpty() specifies a different Observable to emit values from if the source Observable is empty. This allows you specify a different sequence of emissions in the event that the source is empty rather than emitting just one value.
We could choose to emit three additional strings, for example, if the preceding Observable came out empty due to a filter() operation:
import io.reactivex.Observable;public class Launcher { public static void main(String[] args) { Observable.just("Alpha", "Beta", "Gamma", "Delta", "Epsilon") .filter(s -> s.startsWith("Z")) .switchIfEmpty(Observable.just("Zeta", "Eta", "Theta")) .subscribe(i -> System.out.println("RECEIVED: " + i), e -> System.out.println("RECEIVED ...
Read now
Unlock full access