- Learning RxJava
- Thomas Nield
- 135字
- 2021-07-02 22:22:53
Observable.never()
A close cousin of Observable.empty() is Observable.never(). The only difference between them is that it never calls onComplete(), forever leaving observers waiting for emissions but never actually giving any:
import io.reactivex.Observable;
public class Launcher {
public static void main(String[] args) {
Observable<String> empty = Observable.never();
empty.subscribe(System.out::println,
Throwable::printStackTrace,
() -> System.out.println("Done!"));
sleep(5000);
}
public static void sleep(int millis) {
try {
Thread.sleep(millis);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
This Observable is primarily used for testing and not that often in production. We have to use sleep() here just like Observable.interval() because the main thread is not going to wait for it after kicking it off. In this case, we just use sleep() for five seconds to prove that no emissions are coming from it. Then, the application will quit.
推薦閱讀
- Spring 5企業級開發實戰
- Mastering Entity Framework
- OpenStack Cloud Computing Cookbook(Fourth Edition)
- 人臉識別原理及算法:動態人臉識別系統研究
- Flutter跨平臺開發入門與實戰
- Procedural Content Generation for C++ Game Development
- Python程序設計與算法基礎教程(第2版)(微課版)
- IDA Pro權威指南(第2版)
- Python數據可視化之美:專業圖表繪制指南(全彩)
- Vue.js 3應用開發與核心源碼解析
- STM8實戰
- DB2SQL性能調優秘笈
- Clojure Web Development Essentials
- PostgreSQL 12 High Availability Cookbook
- PHP程序設計高級教程