Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Rx: What? Why? How?

Rx: What? Why? How?

With modern applications having a lot of moving parts, asynchrony is ubiquitous! Be it the IO, UI or the database accesses, it has to be fluid for a better user experience. Reactive extensions provides a way to build robust, resilient systems where failures are dealt with elegance rather than disaster.

We take a dive into demystifying Rx; taking a jump into reactive streams & their constructs, junking callbacks in favour of a fluent API, and finding solutions for common problems.

Ashish Krishnan

October 20, 2018
Tweet

More Decks by Ashish Krishnan

Other Decks in Technology

Transcript

  1. Callbacks Alamofire.request(req).responseJSON { (response) in switch response.result { case .success:

    // Store in a database or file systems case .failure(let error): showError() } }
  2. Callbacks Alamofire.request(req).responseJSON { (response) in switch response.result { case .success:

    // Store in a database or file systems case .failure(let error): showError() } }
  3. Under the hood Reactive Streams! Subjects & Observers + can

    signal “I am done”. + can signal error.
  4. A simple chain: Observable .from(1, 2, 2, 2, 5, 6)

    .map { } .skip(2) .distinct() .doOnEach { } .subscribe { }
  5. Observable .from(1, 2, 2, 2, 5, 6) .map { }

    .skip(2) .distinct() .doOnEach { } .subscribe { } Source-like
  6. Observable .from(1, 2, 2, 2, 5, 6) .map { }

    .skip(2) .distinct() .doOnEach { } .subscribe { } observer
  7. Observable .from(1, 2, 2, 2, 5, 6) .map { }

    .skip(2) .distinct() .doOnEach { } .subscribe { onNext ( onError ( onComplete ( } Remember?
  8. Observable .from(1, 2, 2, 2, 5, 6) .map { }

    .skip(2) .distinct() .doOnEach { } .subscribe { } subscription *upstream
  9. Observable .from(1, 2, 2, 2, 5, 6) .map { }

    .skip(2) .distinct() .doOnEach { } .subscribeOn( /* thread pool */) .subscribe { } subscription utility for threading
  10. Observable .from(1, 2, 2, 2, 5, 6) .map { /*

    multiply $0 by 100 */ } .skip(2) .distinct() .doOnEach { } .subscribe { } transforming data
  11. Observable .from(1, 2, 2, 2, 5, 6) .map { }

    .skip(2) .distinct() .doOnEach { } .subscribe { } filtering
  12. Observable .from(1, 2, 2, 2, 5, 6) .map { }

    .skip(2) .distinct() .doOnEach { } .subscribe { }
  13. Observable .from(1, 2, 2, 2, 5, 6) .map { }

    .skip(2) .distinct() .doOnEach { /* print? */ } .subscribe { } Side-effects
  14. Observable .from(1, 2, 2, 2, 5, 6) .map { }

    .skip(2) .distinct() .doOnEach { } .observeOn( /* main? */ ) .subscribe { } = disposable bag emissions Another utility for threading
  15. Callbacks Alamofire.request(req).responseJSON { (response) in switch response.result { case .success:

    // Store in a database or file systems store() { case .success: // do something case .failure: // show error } case .failure(let error): showError() } }
  16. Callback Hell Alamofire.request(req).responseJSON { (response) in switch response.result { case

    .success: // Store in a database or file systems store() { case .success: // do something case .failure: // show error } case .failure(let error): showError() } }
  17. How? “Unless you can model entire system synchronously, a single

    async source breaks imperative programming” Jake Wharton
  18. How? “Unless you can model entire system synchronously, a single

    async source breaks imperative programming” Jake Wharton
  19. How? “Unless you can model entire system synchronously, a single

    async source breaks imperative programming” Jake Wharton
  20. MVC - 1979 MVP - 1996 MVVM - 2005 ...

    Android & iPhone: 2007 - 2009
  21. MVC - 1979 MVP - 1996 MVVM - 2005 ...

    Android & iPhone: 2007 - 2009 ... MVI - 2015