- Spring 5.0 Projects
- Nilang Patel
- 383字
- 2021-07-02 12:35:04
Other ways to get Observable
So far we have seen how to get Observable with just(), create(), and interval(). However, there are other sources to get the Observable. You can get full details about each source from at:https://github.com/ReactiveX/RxJava/wiki/Creating-Observables:
- range: If you want to emit a consecutive range of integers, you can use the Observable.range(int from, int to) call. As its name suggests, it will start emitting a number from the start value in increments until the end count is reached.
- empty: In a rare situation, you need to create Observable that emits nothing and calls onComplete(). In this case, you can use this source type with the Observable.empty() call.
- never: It is equivalent to empty with the difference being that this will never make a call to onComplete() and keep the Observable waiting to emit a state. This is also used less frequently.
- error: If you wish to create Observable that immediately calls onError(), you can use this source with the Observable.error() call. It is used for testing purposes mainly.
- future: It was introduced way back and used as a placeholder for the result that is not yet produced. Observable is more powerful than future but if you are using old libraries, you can convert Observable to future with the Observable.future() call.
- defer: This is basically used to create a separate state for each Observer. It is useful when the source of the stream is stateful. If you want your observers to reflect the changes happening to the Observable state, then you can use this source type with an Observable.defer() call.
- single: This type of Observable just emits a single value and can be used with a Single.just() method call.
- maybe: It is similar to the single type, the only difference that it emits zero or one data at maximum and can be used with a Maybe.just() call.
- fromCallable: If you want to perform certain actions of computation before emitting the data, you can use this source with an Observable.fromCallable() call. In case any error occurs and you want to pass it to the Observable chain through an onError() call instead of throwing the error, you can use this source type.
推薦閱讀
- 物聯網智慧安監技術
- 智能網聯汽車V2X與智能網聯設施I2X
- 網管員典藏書架:網絡管理與運維實戰寶典
- HCNA網絡技術
- SSL VPN : Understanding, evaluating and planning secure, web/based remote access
- Master Apache JMeter:From Load Testing to DevOps
- 端到端QoS網絡設計
- 工業互聯網創新實踐
- 轉化:提升網站流量和轉化率的技巧
- 5G時代的大數據技術架構和關鍵技術詳解
- 物聯網的機遇與利用
- 云工廠:開啟中國制造云時代
- Selenium WebDriver 3 Practical Guide
- 物聯網基礎及應用
- 目標跟蹤中的群智能優化方法