官术网_书友最值得收藏!

Observable.empty()

Although this may not seem useful yet, it is sometimes helpful to create an Observable that emits nothing and calls onComplete():

    import io.reactivex.Observable;

public class Launcher {
public static void main(String[] args) {

Observable<String> empty = Observable.empty();

empty.subscribe(System.out::println,
Throwable::printStackTrace,
() -> System.out.println("Done!"));
}
}

The output is as follows:

    Done!

Note that no emissions were printed because there were none. It went straight to calling onComplete , which printed the Done! message in the Observer. Empty observables are common to represent empty datasets. They can also result from operators such as filter() when all emissions fail to meet a condition. Sometimes, you will deliberately create empty Observables using Observable.empty(), and we will see examples of this in a few places throughout this book.

An empty Observable is essentially RxJava's concept of null. It is the absence of a value (or technically, "values"). Empty Observables are much more elegant than nulls because operations will simply continue empty rather than throw NullPointerExceptions. But when things go wrong in RxJava programs, sometimes it is because observers are receiving no emissions. When this happens, you have to trace through your Observable's chain of operators to find which one caused emissions to become empty.

主站蜘蛛池模板: 济南市| 中阳县| 玛多县| 青海省| 赤城县| 富蕴县| 连云港市| 宁夏| 揭阳市| 阿合奇县| 泊头市| 尚义县| 息烽县| 巢湖市| 嘉鱼县| 石林| 仙居县| 景德镇市| 迭部县| 黑龙江省| 奈曼旗| 乌恰县| 凤台县| 乌拉特中旗| 敖汉旗| 隆子县| 剑阁县| 班玛县| 尉犁县| 临城县| 普兰县| 应用必备| 安多县| 公安县| 吴旗县| 若羌县| 肃北| 怀仁县| 清新县| 长寿区| 台北市|