イマイチなsubscribeをRxぽくする
by
Yuta Kawabe
×
Copy
Open
Share
Embed
Copy iframe code
Copy JS code
Copy link
Start on current slide
Slide 1
Slide 1 text
© and factory,inc. All Rights Reserved. ΠϚΠνͳsubscribeΛ RxΆ͘͢Δ 2019/12/24 ͋Δ͋ΔLT Vol.9
Slide 2
Slide 2 text
© and factory,inc. All Rights Reserved. ͋Δ͋ΔLT Vol.9 | ΠϚΠνͳsubscribeΛRxΆ͘͢Δ Yaruki00 @Yaruki00 @Yaruki_00 ล༟ଠ Yuta Kawabe J04։ൃྺ͘Β͍ ࠷ۙ3YΛͬͨ.77.͕͖ ΫϦεϚεͰࢥ͍ग़͢͜ͱಛʹແ͍ BOEGBDUPSZגࣜձࣾ J04ΤϯδχΞ
Slide 3
Slide 3 text
© and factory,inc. All Rights Reserved. ͋Δ͋ΔLT Vol.9 | ΠϚΠνͳsubscribeΛRxΆ͘͢Δ ݩʹͳͬͨهࣄ ▸ ͍͍Ͷ͍ͩ͘͞ʂ https://qiita.com/Yaruki00/items/3afb8082f6bff0a9fe90
Slide 4
Slide 4 text
© and factory,inc. All Rights Reserved. subscribeʹ ৭ʑॻ͖͕ͪ Rxॳ৺ऀ
Slide 5
Slide 5 text
© 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)
Slide 6
Slide 6 text
© 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) σʔλͷม
Slide 7
Slide 7 text
© 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) Φϓγϣφϧͷ Ξϯϥοϓ
Slide 8
Slide 8 text
© 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) ϑΟϧλϦϯά
Slide 9
Slide 9 text
© 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) Φϒβʔόʔʹ ΤϨϝϯτΛૹΔ
Slide 10
Slide 10 text
© 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)
Slide 11
Slide 11 text
© and factory,inc. All Rights Reserved. ॻ͖͑ͯ ͍͖·͢
Slide 12
Slide 12 text
© 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) σʔλͷม
Slide 13
Slide 13 text
© and factory,inc. All Rights Reserved. ͋Δ͋ΔLT Vol.9 | ΠϚΠνͳsubscribeΛRxΆ͘͢Δ σʔλͷม let optionalInt = dataSource?[indexPath.row] .map { dataSource?[$0.row] } func map(_ transform: @escaping (Self.Element) throws -> Result) -> RxSwift.Observable લ ޙ
Slide 14
Slide 14 text
© 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) Φϓγϣφϧͷ Ξϯϥοϓ
Slide 15
Slide 15 text
© 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(_ selector: @escaping (Self.Element) throws -> Source) -> RxSwift.Observable where Source : RxSwift.ObservableConvertibleType લ ޙ
Slide 16
Slide 16 text
© 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) ϑΟϧλϦϯά
Slide 17
Slide 17 text
© 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 લ ޙ
Slide 18
Slide 18 text
© 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) Φϒβʔόʔʹ ΤϨϝϯτΛૹΔ
Slide 19
Slide 19 text
© and factory,inc. All Rights Reserved. ͋Δ͋ΔLT Vol.9 | ΠϚΠνͳsubscribeΛRxΆ͘͢Δ ΦϒβʔόʔʹΤϨϝϯτΛૹΔ someObserver.on(.next(int)) .bind(to: someObserver) func bind(to observers: Observer...) -> RxSwift.Disposable where Observer : RxSwift.ObserverType, Self.Element == Observer.Element લ ޙ
Slide 20
Slide 20 text
© 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)
Slide 21
Slide 21 text
© 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) σʔλͷม
Slide 22
Slide 22 text
© 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) Φϓγϣφϧͷ Ξϯϥοϓ
Slide 23
Slide 23 text
© 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) ϑΟϧλϦϯά
Slide 24
Slide 24 text
© 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) ϦϨʔʹ ΤϨϝϯτΛૹΔ
Slide 25
Slide 25 text
© 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)
Slide 26
Slide 26 text
© and factory,inc. All Rights Reserved. ͋Δ͋ΔLT Vol.9 | ΠϚΠνͳsubscribeΛRxΆ͘͢Δ ศརͳΦϖϨʔλʔͨ͘͞Μ͋Δ ▸ http://reactivex.io/documentation/operators.html ▸ merge ▸ distinct ▸ skip, take ▸ zip ▸ observeOn, subscribeOn ▸ ͳͲͳͲ
Slide 27
Slide 27 text
© and factory,inc. All Rights Reserved. ΦϖϨʔλʔΛ ͍͜ͳͯ͠ ָ͍͠RxϥΠϑΛʂ