right. The Observer pattern done right. ReactiveX is a combination ReactiveX is a combination of the best ideas of the best ideas from the from the Observer Observer pattern, pattern, the the Iterator Iterator pattern, pattern, and and functional programming functional programming 2
observable streams. Less is more ReactiveX's operators often reduce what was once an elaborate challenge into a few lines of code. Async error handling Traditional try/catch is powerless for errors in asynchronous computations, but ReactiveX is equipped with proper mechanisms for handling errors. Concurrency made easy Observables and Schedulers in ReactiveX allow the programmer to abstract away low-level threading, synchronization, and concurrency issues. Features Features 3
"hot" to "cold" and vice versa AsyncSubject (emits last set of items) BehaviorSubject (emits most recent and beyond) PublishSubject (emits only source's) ReplaySubject (emits everything) Types Types 9
1 var b = 2 if a + b >= 0 { c = "\(a + b) is positive" } let a /*: Observable<Int>*/ = Variable(1) let b /*: Observable<Int>*/ = Variable(2) let c = Observable.combineLatest(a, b) { $0 + $1 } .filter { $0 >= 0 } .map { "\($0) is positive" } .subscribeNext { print($0) } Traditional RxSwift 15
Disposable */ = primeTextField.rx_text // type is Observable<Observable<Prime>> .map { WolframAlphaIsPrime($0.toInt() ?? 0) } // type is Observable<Prime> .concat() // type is Observable<String> .map { "number \($0.n) is prime? \($0.isPrime)" } // return Disposable that can be used to unbind everything .bindTo(resultLabel.rx_text) .addDisposableTo(disposableBag) // This will set resultLabel.text to "number 43 is prime? true" after // server call completes. primeTextField.text = "43" 16