- Learning RxJava
- Thomas Nield
- 205字
- 2021-07-02 22:22:53
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.
- C語言程序設(shè)計(jì)
- Full-Stack Vue.js 2 and Laravel 5
- Spring實(shí)戰(zhàn)(第5版)
- JSP開發(fā)案例教程
- 微信小程序項(xiàng)目開發(fā)實(shí)戰(zhàn)
- SQL基礎(chǔ)教程(第2版)
- Unity&VR游戲美術(shù)設(shè)計(jì)實(shí)戰(zhàn)
- Node.js 12實(shí)戰(zhàn)
- Troubleshooting Citrix XenApp?
- Learning Nessus for Penetration Testing
- 深入解析Java編譯器:源碼剖析與實(shí)例詳解
- ROS機(jī)器人編程實(shí)戰(zhàn)
- Flink核心技術(shù):源碼剖析與特性開發(fā)
- Java語言程序設(shè)計(jì)實(shí)用教程(第2版)
- Web前端測試與集成:Jasmine/Selenium/Protractor/Jenkins的最佳實(shí)踐