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

イマイチなsubscribeをRxぽくする

Avatar for Yuta Kawabe Yuta Kawabe
December 24, 2019

 イマイチなsubscribeをRxぽくする

Avatar for Yuta Kawabe

Yuta Kawabe

December 24, 2019
Tweet

More Decks by Yuta Kawabe

Other Decks in Programming

Transcript

  1. © and factory,inc. All Rights Reserved. ͋Δ͋ΔLT Vol.9 | ΠϚΠνͳsubscribeΛRxΆ͘͢Δ

    Yaruki00 @Yaruki00 @Yaruki_00 ઒ล༟ଠ Yuta Kawabe J04։ൃྺ೥͘Β͍ ࠷ۙ͸3YΛ࢖ͬͨ.77.͕޷͖ ΫϦεϚεͰࢥ͍ग़͢͜ͱ͸ಛʹແ͍ BOEGBDUPSZגࣜձࣾ J04ΤϯδχΞ
  2. © and factory,inc. All Rights Reserved. ͋Δ͋ΔLT Vol.9 | ΠϚΠνͳsubscribeΛRxΆ͘͢Δ

    ݩʹͳͬͨهࣄ ▸ ͍͍Ͷ͍ͩ͘͞ʂ https://qiita.com/Yaruki00/items/3afb8082f6bff0a9fe90
  3. © and factory,inc. All Rights Reserved. ͋Δ͋ΔLT Vol.9 | ΠϚΠνͳsubscribeΛRxΆ͘͢Δ

    ΠϚΠνͳsubscribe tableView.rx.itemSelected .subscribe(onNext: { indexPath in let optionalInt = dataSource?[indexPath.row] guard let int = optionalInt else { return } if int > 0 { someObserver.on(.next(int)) } }) .disposed(by: disposeBag)
  4. © and factory,inc. All Rights Reserved. ͋Δ͋ΔLT Vol.9 | ΠϚΠνͳsubscribeΛRxΆ͘͢Δ

    ΠϚΠνͳsubscribe tableView.rx.itemSelected .subscribe(onNext: { indexPath in let optionalInt = dataSource?[indexPath.row] guard let int = optionalInt else { return } if int > 0 { someObserver.on(.next(int)) } }) .disposed(by: disposeBag) σʔλͷม׵
  5. © and factory,inc. All Rights Reserved. ͋Δ͋ΔLT Vol.9 | ΠϚΠνͳsubscribeΛRxΆ͘͢Δ

    ΠϚΠνͳsubscribe tableView.rx.itemSelected .subscribe(onNext: { indexPath in let optionalInt = dataSource?[indexPath.row] guard let int = optionalInt else { return } if int > 0 { someObserver.on(.next(int)) } }) .disposed(by: disposeBag) Φϓγϣφϧͷ Ξϯϥοϓ
  6. © and factory,inc. All Rights Reserved. ͋Δ͋ΔLT Vol.9 | ΠϚΠνͳsubscribeΛRxΆ͘͢Δ

    ΠϚΠνͳsubscribe tableView.rx.itemSelected .subscribe(onNext: { indexPath in let optionalInt = dataSource?[indexPath.row] guard let int = optionalInt else { return } if int > 0 { someObserver.on(.next(int)) } }) .disposed(by: disposeBag) ϑΟϧλϦϯά
  7. © and factory,inc. All Rights Reserved. ͋Δ͋ΔLT Vol.9 | ΠϚΠνͳsubscribeΛRxΆ͘͢Δ

    ΠϚΠνͳsubscribe tableView.rx.itemSelected .subscribe(onNext: { indexPath in let optionalInt = dataSource?[indexPath.row] guard let int = optionalInt else { return } if int > 0 { someObserver.on(.next(int)) } }) .disposed(by: disposeBag) Φϒβʔόʔʹ ΤϨϝϯτΛૹΔ
  8. © and factory,inc. All Rights Reserved. ͋Δ͋ΔLT Vol.9 | ΠϚΠνͳsubscribeΛRxΆ͘͢Δ

    ΠϚΠνͳsubscribe tableView.rx.itemSelected .subscribe(onNext: { indexPath in let optionalInt = dataSource?[indexPath.row] guard let int = optionalInt else { return } if int > 0 { someObserver.on(.next(int)) } }) .disposed(by: disposeBag)
  9. © and factory,inc. All Rights Reserved. ͋Δ͋ΔLT Vol.9 | ΠϚΠνͳsubscribeΛRxΆ͘͢Δ

    σʔλͷม׵ tableView.rx.itemSelected .subscribe(onNext: { indexPath in let optionalInt = dataSource?[indexPath.row] guard let int = optionalInt else { return } if int > 0 { someObserver.on(.next(int)) } }) .disposed(by: disposeBag) σʔλͷม׵
  10. © and factory,inc. All Rights Reserved. ͋Δ͋ΔLT Vol.9 | ΠϚΠνͳsubscribeΛRxΆ͘͢Δ

    σʔλͷม׵ let optionalInt = dataSource?[indexPath.row] .map { dataSource?[$0.row] } func map<Result>(_ transform: @escaping (Self.Element) throws -> Result) -> RxSwift.Observable<Result> લ ޙ
  11. © and factory,inc. All Rights Reserved. ͋Δ͋ΔLT Vol.9 | ΠϚΠνͳsubscribeΛRxΆ͘͢Δ

    ΦϓγϣφϧͷΞϯϥοϓ tableView.rx.itemSelected .subscribe(onNext: { indexPath in let optionalInt = dataSource?[indexPath.row] guard let int = optionalInt else { return } if int > 0 { someObserver.on(.next(int)) } }) .disposed(by: disposeBag) Φϓγϣφϧͷ Ξϯϥοϓ
  12. © and factory,inc. All Rights Reserved. ͋Δ͋ΔLT Vol.9 | ΠϚΠνͳsubscribeΛRxΆ͘͢Δ

    ΦϓγϣφϧͷΞϯϥοϓ guard let int = optionalInt else { return } .flatMap { $0.flatMap(Observable.just) ?? .empty() } func flatMap<Source>(_ selector: @escaping (Self.Element) throws -> Source) -> RxSwift.Observable<Source.Element> where Source : RxSwift.ObservableConvertibleType લ ޙ
  13. © and factory,inc. All Rights Reserved. ͋Δ͋ΔLT Vol.9 | ΠϚΠνͳsubscribeΛRxΆ͘͢Δ

    ϑΟϧλϦϯά tableView.rx.itemSelected .subscribe(onNext: { indexPath in let optionalInt = dataSource?[indexPath.row] guard let int = optionalInt else { return } if int > 0 { someObserver.on(.next(int)) } }) .disposed(by: disposeBag) ϑΟϧλϦϯά
  14. © and factory,inc. All Rights Reserved. ͋Δ͋ΔLT Vol.9 | ΠϚΠνͳsubscribeΛRxΆ͘͢Δ

    ϑΟϧλϦϯά if int > 0 { … } .filter { $0 > 0 } func filter(_ predicate: @escaping (Self.Element) throws -> Bool) -> RxSwift.Observable<Self.Element> લ ޙ
  15. © and factory,inc. All Rights Reserved. ͋Δ͋ΔLT Vol.9 | ΠϚΠνͳsubscribeΛRxΆ͘͢Δ

    ΦϒβʔόʔʹΤϨϝϯτΛૹΔ tableView.rx.itemSelected .subscribe(onNext: { indexPath in let optionalInt = dataSource?[indexPath.row] guard let int = optionalInt else { return } if int > 0 { someObserver.on(.next(int)) } }) .disposed(by: disposeBag) Φϒβʔόʔʹ ΤϨϝϯτΛૹΔ
  16. © and factory,inc. All Rights Reserved. ͋Δ͋ΔLT Vol.9 | ΠϚΠνͳsubscribeΛRxΆ͘͢Δ

    ΦϒβʔόʔʹΤϨϝϯτΛૹΔ someObserver.on(.next(int)) .bind(to: someObserver) func bind<Observer>(to observers: Observer...) -> RxSwift.Disposable where Observer : RxSwift.ObserverType, Self.Element == Observer.Element લ ޙ
  17. © and factory,inc. All Rights Reserved. ͋Δ͋ΔLT Vol.9 | ΠϚΠνͳsubscribeΛRxΆ͘͢Δ

    Ξϑλʔ tableView.rx.itemSelected .map { dataSource?[$0.row] } .flatMap { $0.flatMap(Observable.just) ?? .empty() } .filter { $0 > 0 } .bind(to: someObserver) .disposed(by: disposeBag)
  18. © and factory,inc. All Rights Reserved. ͋Δ͋ΔLT Vol.9 | ΠϚΠνͳsubscribeΛRxΆ͘͢Δ

    Ξϑλʔ tableView.rx.itemSelected .map { dataSource?[$0.row] } .flatMap { $0.flatMap(Observable.just) ?? .empty() } .filter { $0 > 0 } .bind(to: someObserver) .disposed(by: disposeBag) σʔλͷม׵
  19. © and factory,inc. All Rights Reserved. ͋Δ͋ΔLT Vol.9 | ΠϚΠνͳsubscribeΛRxΆ͘͢Δ

    Ξϑλʔ tableView.rx.itemSelected .map { dataSource?[$0.row] } .flatMap { $0.flatMap(Observable.just) ?? .empty() } .filter { $0 > 0 } .bind(to: someObserver) .disposed(by: disposeBag) Φϓγϣφϧͷ Ξϯϥοϓ
  20. © and factory,inc. All Rights Reserved. ͋Δ͋ΔLT Vol.9 | ΠϚΠνͳsubscribeΛRxΆ͘͢Δ

    Ξϑλʔ tableView.rx.itemSelected .map { dataSource?[$0.row] } .flatMap { $0.flatMap(Observable.just) ?? .empty() } .filter { $0 > 0 } .bind(to: someObserver) .disposed(by: disposeBag) ϑΟϧλϦϯά
  21. © and factory,inc. All Rights Reserved. ͋Δ͋ΔLT Vol.9 | ΠϚΠνͳsubscribeΛRxΆ͘͢Δ

    Ξϑλʔ tableView.rx.itemSelected .map { dataSource?[$0.row] } .flatMap { $0.flatMap(Observable.just) ?? .empty() } .filter { $0 > 0 } .bind(to: someObserver) .disposed(by: disposeBag) ϦϨʔʹ ΤϨϝϯτΛૹΔ
  22. © and factory,inc. All Rights Reserved. ͋Δ͋ΔLT Vol.9 | ΠϚΠνͳsubscribeΛRxΆ͘͢Δ

    ϏϑΥΞ&Ξϑλʔ tableView.rx.itemSelected .map { dataSource?[$0.row] } .flatMap { $0.flatMap(Observable.just) ?? .empty() } .filter { $0 > 0 } .bind(to: someObserver) .disposed(by: disposeBag) tableView.rx.itemSelected .subscribe(onNext: { indexPath in let optionalInt = dataSource?[indexPath.row] guard let int = optionalInt else { return } if int > 0 { someObserver.on(.next(int)) } }) .disposed(by: disposeBag)
  23. © and factory,inc. All Rights Reserved. ͋Δ͋ΔLT Vol.9 | ΠϚΠνͳsubscribeΛRxΆ͘͢Δ

    ศརͳΦϖϨʔλʔ͸ͨ͘͞Μ͋Δ ▸ http://reactivex.io/documentation/operators.html ▸ merge ▸ distinct ▸ skip, take ▸ zip ▸ observeOn, subscribeOn ▸ ͳͲͳͲ