RxSwift: Deep Cuts

RxSwift: Deep Cuts

43d2bef703ec7165166f161f137ac54f?s=128

Krzysztof Siejkowski

October 21, 2017
Tweet

Transcript

  1. 5.
  2. 6.

    dataProvider.refreshData() .subscribe( onNext: { [weak self] in self!?.update(with: $0) },

    onError: { [weak self] in if let error = $0 as? ReasonableError { self!?.showUserMessage(with: error.reason) } } ) .disposed(by: disposeBag)
  3. 7.

    dataProvider.refreshData() .subscribe( onNext: { [weak self] in self!?.update(with: $0) },

    onError: { [weak self] in if let error = $0 as? ReasonableError { self!?.showUserMessage(with: error.reason) } } ) .disposed(by: disposeBag)
  4. 8.

    dataProvider.refreshData() .subscribe( onNext: { [weak self] in self!?.update(with: $0) },

    onError: { [weak self] in if let error = $0 as? ReasonableError { self!?.showUserMessage(with: error.reason) } } ) .disposed(by: disposeBag)
  5. 9.

    dataProvider.refreshData() .subscribe( onNext: { [weak self] in self!?.update(with: $0) },

    onError: { [weak self] in if let error = $0 as? ReasonableError { self!?.showUserMessage(with: error.reason) } } ) .disposed(by: disposeBag)
  6. 10.

    dataProvider.refreshData() .subscribe( onNext: { [weak self] in self!?.update(with: $0) },

    onError: { [weak self] in if let error = $0 as? ReasonableError { self!?.showUserMessage(with: error.reason) } } ) .disposed(by: disposeBag)
  7. 11.

    dataProvider.refreshData() .subscribe( onNext: { [weak self] in self!?.update(with: $0) },

    onError: { [weak self] in if let error = $0 as? ReasonableError { self!?.showUserMessage(with: error.reason) } } ) .disposed(by: disposeBag)
  8. 15.

    Observable .just(42) .filter { $0 > 30 } .map {

    “\($0)” } Just Filter Map
  9. 16.

    Observable .just(42) .filter { $0 > 30 } .map {

    “\($0)” } .distinctUntilChanged { $0 !== $1 } Just Filter Map Distinct UntilChanged
  10. 17.

    let observable: Observable<String> = Observable .just(42) .filter { $0 >

    30 } .map { “\($0)” } .distinctUntilChanged { $0 !== $1 } Just Filter Map observable observable
  11. 18.

    environment Observable .just(42) .filter { $0 > 30 } .map

    { “\($0)” } .distinctUntilChanged { $0 !== $1 } Just Filter Map Distinct UntilChanged filter closure compare closure map closure
  12. 19.

    Observable .just(42) .filter { $0 > 30 } .map {

    “\($0)” } .distinctUntilChanged { $0 !== $1 } .subscribe { print($0) } Just Filter Map Distinct UntilChanged filter closure compare closure map closure Just Sink Filter Sink Map Sink DUC Sink
  13. 20.

    Observable .just(42) .filter { $0 > 30 } .map {

    “\($0)” } .distinctUntilChanged { $0 !== $1 } .subscribe { print($0) } Just Filter Map Distinct UntilChanged filter closure compare closure map closure Just Sink Filter Sink Map Sink DUC Sink
  14. 21.

    Observable .just(42) .filter { $0 > 30 } .map {

    “\($0)” } .distinctUntilChanged { $0 !== $1 } .subscribe { print($0) } Just Filter Map Distinct UntilChanged filter closure compare closure map closure Just Sink Filter Sink Map Sink DUC Sink Observer Observer
  15. 22.

    filter closure compare closure map closure Just Sink Filter Sink

    Map Sink DUC Sink Observer Just Sink Disposer Filter Sink Disposer Map Sink Disposer DUC Sink Disposer Observable .just(42) .filter { $0 > 30 } .map { “\($0)” } .distinctUntilChanged { $0 !== $1 } .subscribe { print($0) }
  16. 23.

    reference cycles filter closure compare closure map closure Just Sink

    Filter Sink Map Sink DUC Sink Observer Just Sink Disposer Filter Sink Disposer Map Sink Disposer DUC Sink Disposer
  17. 24.

    filter closure compare closure map closure Just Sink Filter Sink

    Map Sink DUC Sink Observer Just Sink Disposer Filter Sink Disposer Map Sink Disposer disposable let disposable: Disposable = Observable .just(42) .filter { $0 > 30 } .map { “\($0)” } .distinctUntilChanged { $0 !== $1 } .subscribe { print($0) }
  18. 25.

    disposable .dispose() filter closure compare closure map closure Just Sink

    Filter Sink Map Sink DUC Sink Observer Just Sink Disposer Filter Sink Disposer Map Sink Disposer disposable
  19. 27.

    filter closure compare closure map closure Just Sink Filter Sink

    Map Sink DUC Sink Observer Just Sink Disposer Filter Sink Disposer Map Sink Disposer disposable observe closure let disposable = Observable .just(42) .filter { $0 > 30 } .map { “\($0)” } .distinctUntilChanged { $0 !== $1 } .subscribe { print($0) }
  20. 28.

    environment let disposable = Observable .just(42) .filter { $0 >

    30 } .map { “\($0)” } .distinctUntilChanged { $0 !== $1 } .subscribe { print($0) } filter closure compare closure map closure Just Sink Filter Sink Map Sink DUC Sink Observer Just Sink Disposer Filter Sink Disposer Map Sink Disposer disposable observe closure
  21. 32.

    observable .map { [weak self] in self!?.transform($0) } .subscribe {

    [weak self] in self!?.react(to: $0) } observable .map { [unowned self] in self.transform($0) } .subscribe { [unowned self] in self.react(to: $0) } .disposed( by: self.disposeBag )
  22. 33.

    !// instance method func foo(_ bar: Int) !-> Int !//

    instance scope Observable.just(42) .map(foo) !// instance method func foo(_ bar: Int) !-> Int !// instance scope Observable.just(42) .map { self.foo($0) }
  23. 34.

    dataProvider.refreshData() .subscribe( onNext: { [weak self] in self!?.update(with: $0) },

    onError: { [weak self] in if let error = $0 as? ReasonableError { self!?.showUserMessage(with: error.reason) } } ) .disposed(by: disposeBag)
  24. 35.

    dataProvider.refreshData() .subscribe( onNext: { [unowned self] in self.update(with: $0) },

    onError: { [unowned self] in if let error = $0 as? ReasonableError { self.showUserMessage(with: error.reason) } } ) .disposed(by: disposeBag)
  25. 36.

    dataProvider.refreshData() .subscribe( onNext: { [unowned self] in self.update(with: $0) },

    onError: { [unowned self] in if let error = $0 as? ReasonableError { self.showUserMessage(with: error.reason) } } ) .disposed(by: disposeBag)
  26. 40.

    MainScheduler let mainQueue = DispatchQueue.main !// simplified essence of schedule

    method if DispatchQueue.isMain { action(state) } else { mainQueue.async { action(state) } }
  27. 41.

    SerialDispatchQueueScheduler !// internal serial dispatch queue of given properties let

    queue = DispatchQueue.global(qos: qos.qosClass) !// simplified essence of schedule method queue.async { action(state) }
  28. 42.

    OperationQueueScheduler !// operation queue provided by client in the initializer

    let operationQueue: OperationQueue !// simplified essence of schedule method operationQueue.addOperation( BlockOperation { action(state)) } )
  29. 43.

    Also in schedulers • QOS • serial / concurrent •

    immediate / delayed • driven by client
  30. 47.

    Observable .just(42) .filter { $0 > 33 } .map {

    "\($0)" } .subscribe { print($0) } .disposed(by: disposeBag) Subscription thread
  31. 48.

    Observable .just(42) .observeOn(greenScheduler) .filter { $0 > 33 } .map

    { "\($0)" } .subscribe { print($0) } .disposed(by: disposeBag) Subscription thread Green scheduler
  32. 49.

    Observable .just(42) .observeOn(greenScheduler) .filter { $0 > 33 } .observeOn(redScheduler)

    .map { "\($0)" } .observeOn(greenScheduler) .subscribe { print($0) } .disposed(by: disposeBag) Subscription thread Red scheduler Green scheduler Green scheduler
  33. 50.

    Observable .just(42) .observeOn(greenScheduler) .filter { $0 > 33 } .observeOn(redScheduler)

    .subscribeOn(blueScheduler) .map { "\($0)" } .observeOn(greenScheduler) .subscribe { print($0) } .disposed(by: disposeBag) Blue scheduler Red scheduler Green scheduler Green scheduler
  34. 51.

    Observable .just(42) .observeOn(greenScheduler) .filter { $0 > 33 } .subscribeOn(blueScheduler)

    .observeOn(redScheduler) .subscribeOn(redScheduler) .map { "\($0)" } .observeOn(greenScheduler) .subscribeOn(greenScheduler) .subscribe { print($0) } .disposed(by: disposeBag) Blue scheduler Red scheduler Green scheduler Green scheduler
  35. 52.

    Observable .just( 42, scheduler: blueScheduler ) .observeOn(greenScheduler) .filter { $0

    > 33 } .observeOn(redScheduler) .subscribeOn(redScheduler) .map { "\($0)" } .observeOn(greenScheduler) .subscribe { print($0) } .disposed(by: disposeBag) Blue scheduler Red scheduler Green scheduler Green scheduler
  36. 54.

    Schedule-using for generation !// Scheduler to send elements on. .fromScheduled([1,

    2, 3], greenScheduler) !// Scheduler to run the producer loop on. .repeatElement(1, blueScheduler) !// Scheduler to run the timer on. .interval(1, redScheduler)
  37. 55.

    Schedule-using for internal work !// Scheduler to run the throttle

    timers on. .throttle(1, blueScheduler) !// Scheduler to run the subscription delay timer on. .delay(1, greenScheduler) !// Scheduler to run buffering timers on. .buffer(timeSpan: 1, count: 3, scheduler: redScheduler)
  38. 57.

    Observable<Int> .merge([ Observable<Int>.interval(3, redScheduler), Observable<Int>.interval(5, greenScheduler), ]) .subscribe { [unowned

    self] in !// sometimes red, sometimes green self.work(with: $0) } .disposed(by: disposeBag)
  39. 61.

    dataProvider.refreshData() .observeOn(MainScheduler.instance) .subscribe( onNext: { [unowned self] in self.update(with: $0)

    }, onError: { [unowned self] in if let error = $0 as? ReasonableError { self.showUserMessage(with: error.reason) } } ) .disposed(by: disposeBag)
  40. 62.

    dataProvider.refreshData() .observeOn(MainScheduler.instance) .subscribe( onNext: { [unowned self] in self.update(with: $0)

    }, onError: { [unowned self] in if let error = $0 as? ReasonableError { self.showUserMessage(with: error.reason) } } ) .disposed(by: disposeBag)
  41. 67.

    Finishing !// you may ignore disposable (though I recommend not

    to!) _ = observable .subscribe(onNext: { [weak self] in self!?.work(on: $0) })
  42. 68.

    Not finishing let disposable = observable .subscribe(onNext: { [weak self]

    in self!?.work(on: $0) }) !// ensure subscription disposing disposable.dispose() disposable.disposed(by: disposeBag)
  43. 70.

    One-off let serial = SerialDisposable() !// call each time you

    want the fresh data func fetchFreshData() { serial.disposable = observable .subscribe(onNext: { [unowned self] in self.work(on: $0) }) }
  44. 71.

    Updating let disposeBag = DisposeBag() !// call only once in

    the object lifetime func listenForFreshData() { observable .subscribe(onNext: { [unowned self] in self.work(on: $0) }) .disposed(by: disposeBag) }
  45. 73.

    Immediately & synchronously !// if not ready to handle data

    at the `subscribe` time dataProvider .data() .skip(1) .subscribe(onNext: { [unowned self] in self.work(on: $0) }) .disposed(by: disposeBag)
  46. 74.

    After some time & asynchronously !// provide separate method for

    fetching initial data let initialData = dataProvider.getInitialData() dataProvider .dataUpdates() .subscribe(onNext: { [unowned self] in self.work(on: $0) }) .disposed(by: disposeBag)
  47. 76.

    Not caching let observable = dataProvider .fetchHugeAmountOfDataFromNetwork() !// costly network

    request with fresh data observable.subscribe() !// costly network request with fresh data observable.subscribe()
  48. 77.

    Caching let observable = dataProvider .fetchHugeAmountOfDataFromNetwork() .shareReplay(1) !// costly network

    request with fresh data observable.subscribe() !// no network refresh but also old data observable.subscribe()
  49. 83.

    !/* Returns an Observable that emits at most three times,

    starting with the first event emitted immediately and synchronously upon subscription. Times of other two events are not guaranteed. May not complete, but never errors out. Doesn’t cache any data. !*/ public func thirdTimeLucky() !-> Observable<Data>
  50. 85.

    final class DataProvider { private let proxySubject = PublishSubject<Data>() var

    data: Observable<Data> { return proxySubject.asObservable() } func refreshData() !-> Observable<Void> { return networkService .requestData() .do(onNext: { proxySubject.onNext($0) } .map { _ in } }
  51. 92.

    dataProvider.refreshData() .observeOn(MainScheduler.instance) .subscribe( onNext: { [unowned self] in self.update(with: $0)

    }, onError: { [unowned self] in if let error = $0 as? ReasonableError { self.showUserMessage(with: error.reason) } } ) .disposed(by: disposeBag)
  52. 93.