Share
Embed
イマイチなsubscribeをRxぽくする
Yuta Kawabe
December 24, 2019
Transcript
27 slides
-
© and factory,inc. All Rights Reserved. ͋Δ͋ΔLT Vol.9 | ΠϚΠνͳsubscribeΛRxΆ͘͢Δ
Yaruki00 @Yaruki00 @Yaruki_00 ล༟ଠ Yuta Kawabe J04։ൃྺ͘Β͍ ࠷ۙ3YΛͬͨ.77.͕͖ ΫϦεϚεͰࢥ͍ग़͢͜ͱಛʹແ͍ BOEGBDUPSZגࣜձࣾ J04ΤϯδχΞ -
© and factory,inc. All Rights Reserved. ͋Δ͋ΔLT Vol.9 | ΠϚΠνͳsubscribeΛRxΆ͘͢Δ
ݩʹͳͬͨهࣄ ▸ ͍͍Ͷ͍ͩ͘͞ʂ https://qiita.com/Yaruki00/items/3afb8082f6bff0a9fe90 -
© 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) -
© 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) σʔλͷม -
© 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) Φϓγϣφϧͷ Ξϯϥοϓ -
© 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) ϑΟϧλϦϯά -
© 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) Φϒβʔόʔʹ ΤϨϝϯτΛૹΔ -
© 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) -
© 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) σʔλͷม -
© 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> લ ޙ -
© 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) Φϓγϣφϧͷ Ξϯϥοϓ -
© 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 લ ޙ -
© 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) ϑΟϧλϦϯά -
© 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> લ ޙ -
© 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) Φϒβʔόʔʹ ΤϨϝϯτΛૹΔ -
© 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 લ ޙ -
© 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) -
© 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) σʔλͷม -
© 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) Φϓγϣφϧͷ Ξϯϥοϓ -
© 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) ϑΟϧλϦϯά -
© 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) ϦϨʔʹ ΤϨϝϯτΛૹΔ -
© 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) -
© and factory,inc. All Rights Reserved. ͋Δ͋ΔLT Vol.9 | ΠϚΠνͳsubscribeΛRxΆ͘͢Δ
ศརͳΦϖϨʔλʔͨ͘͞Μ͋Δ ▸ http://reactivex.io/documentation/operators.html ▸ merge ▸ distinct ▸ skip, take ▸ zip ▸ observeOn, subscribeOn ▸ ͳͲͳͲ