February 2020
Intermediate to advanced
412 pages
9h 36m
English
Similar to defaultIfEmpty(), switchIfEmpty() specifies a different Observable to emit values from if the source Observable is empty. This allows you to specify a different sequence of emissions in the event that the source is empty rather than emitting just one value, as in the case of defaultIfEmpty().
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.rxjava3.core.Observable;public class Ch3_04 { public static void main(String[] args) { Observable.just("Alpha", "Beta", "Gamma") .filter(s -> s.startsWith("Z")) .switchIfEmpty(Observable.just("Zeta", "Eta", "Theta")) .subscribe(i -> System.out.println("RECEIVED: " + i),
Read now
Unlock full access