From c6a1f799b02493be02cf003a22ae9ffa960b26eb Mon Sep 17 00:00:00 2001 From: Krunoslav Zaher Date: Sat, 12 Mar 2016 20:30:09 +0100 Subject: [PATCH] Improves `Tips.md`. --- Documentation/Tips.md | 30 +++++++++++++++++++++++++++++- 1 file changed, 29 insertions(+), 1 deletion(-) diff --git a/Documentation/Tips.md b/Documentation/Tips.md index eeb52a31..927687a8 100644 --- a/Documentation/Tips.md +++ b/Documentation/Tips.md @@ -19,4 +19,32 @@ extension ObservableType where E: MaybeCool { } ``` - * Rx operators are as general as possible, but there will always be edge cases that will be hard to model. In those cases + * Rx operators are as general as possible, but there will always be edge cases that will be hard to model. In those cases you can just create your own operator and possibly use one of the built-in operators as a reference. + + * Always use operators to compose subscriptions. + + **Avoid nesting subscribe calls at all cost. This is a bad smell.** + + ```swift + textField.rx_text.subscribeNext { text in + performURLRequest(text).subscribeNext { result in + ... + } + .addDisposableTo(disposeBag) + } + .addDisposableTo(disposeBag) + ``` + + **Preferred way of chaining disposables by using operators.** + + ```swift + textField.rx_text + .flatMapLatest { text in + // Assuming this doesn't fail and returns result on main scheduler, + // otherwise `catchError` and `observeOn(MainScheduler.instance)` can be used to + // correct this. + return performURLRequest(text) + } + ... + .addDisposableTo(disposeBag) // only one top most disposable + ```