Tips

  • Always strive to model your systems or their parts as pure functions. Those pure functions can be tested easily and can be used to modify operator behaviors.
  • When you are using Rx, first try to compose built-in operators.
  • If using some combination of operators often, create your convenience operators.

e.g.

  1. extension ObservableType where E: MaybeCool {
  2. @warn_unused_result(message="http://git.io/rxs.uo")
  3. public func coolElements()
  4. -> Observable<E> {
  5. return filter { e -> Bool in
  6. return e.isCool
  7. }
  8. }
  9. }
  • 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 code smell.

    1. textField.rx_text.subscribeNext { text in
    2. performURLRequest(text).subscribeNext { result in
    3. ...
    4. }
    5. .addDisposableTo(disposeBag)
    6. }
    7. .addDisposableTo(disposeBag)

    Preferred way of chaining disposables by using operators.

    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