Tips
- 总是力求为你的系统或其他部分构建模型,成为纯函数(pure functions)。纯函数易于被测试并且能被用来修改操作符的行为。
- 当你在使用 Rx,首先尝试使用组合内置操作符。
- 如果你经常使用一些操作符的组合,那么封装你自己的操作符
例如:
1. extension ObservableType where E: MaybeCool {
3. @warn_unused_result(message="http://git.io/rxs.uo")
4. public func coolElements()
5. -> Observable<E> {
6. return filter { e -> Bool in
7. return e.isCool
8. }
9. }
10. }
- Rx 操作符尽可能的通用,但是总有边缘用例难以作模型。如果是这样,你可以创建你自己的操作符并且可能使用内置的操作符作为参考。
- 总是使用操作符去构成订阅。
避免嵌套定订阅调用。例如:
1. textField.rx_text.subscribeNext { text in
2. performURLRequest(text).subscribeNext { result in
3. ...
4. }
5. .addDisposableTo(disposeBag)
6. }
7. .addDisposableTo(disposeBag)
更好的方式是使用操作符链式调用
1. textField.rx_text
2. .flatMapLatest { text in
3. // Assuming this doesn't fail and returns result on main scheduler,
4. // otherwise `catchError` and `observeOn(MainScheduler.instance)` can be used to
5. // correct this.
6. return performURLRequest(text)
7. }
8. ...
9. .addDisposableTo(disposeBag) // only one top most disposable
