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

RxSwift Operator 図鑑

RxSwift Operator 図鑑

yidev 第27回勉強会の発表資料です。
share() vs shareReplay() vs shareReplayLatestWhileConnected() のところのデモコードは以下に上げてあります。
https://github.com/toshi0383/HelloRx

E2c86baa99178b8400a2ef49ef861a6c?s=128

toshi0383

April 15, 2017
Tweet

Transcript

  1. RxSwift Operator ਤؑ yidev ୈ̎̓ճษڧձ Apr 15, 2017 Thu. Toshihiro

    Suzuki © Toshihiro Suzuki 2017 1
  2. ࣗݾ঺հ ☞ @toshi0383 ☞ LGTM, TVMLKitchen, Async ... ☞ ࢓ࣄͰ͸ಈը഑৴ܥͷiOS/tvOSΞϓϦ

    © Toshihiro Suzuki 2017 2
  3. yidev yidev #22 2016/03/05 "Being a tvOS developer" https://speakerdeck.com/toshi0383/being-a-tvos-developer ©

    Toshihiro Suzuki 2017 3
  4. 3݄ʹೖࣾ͠·ͨ͠ ☞ ը໘΋ͪΐͪ͜ΐ͜৮ΓͭͭɺओʹࣗಈԽ୲౰. ☞ CocoaPods => Carthage ☞ JenkinsΛ୤͍ͨ͠ ©

    Toshihiro Suzuki 2017 4
  5. ࿩͢͜ͱ ☞ ࢓ࣄ΍WebͰग़ձͬͨRxSwiftͷ৭ʑͳॻ͖ํʹ͍ͭͯɺ৭ʑௐ ΂ͯΈͨ͜ͱΛΏΔ͘ڞ༗͠·͢. ☞ ౰ํ͸ͦ͜·Ͱৄ͍͠Θ͚Ͱ΋ͳ͍Ͱ͢. RxSwiftྺ͸2೥໨͘Β ͍. ☞ ࣗ෼͸͜͏͍͏෩ʹ࢖͍ͬͯΔͱ͍͏ࣄྫͱ͔ɺൃද಺༰Ͱؒ

    ҧ͍ͬͯΔ͜ͱͱ͔͋ͬͨΒͥͻڭ͍͑ͯͩ͘͞! © Toshihiro Suzuki 2017 5
  6. ͓͠ͳ͕͖ ☞ share() vs shareReplay() vs shareReplayLatestWhileConnected() ☞ combineLatest vs

    withLatestFrom ☞ .of() vs .just() / .empty() vs .never() ☞ Observable vs Variable vs Driver vs Subject vs ... ☞ do(onNext:) vs bindNext() vs bindTo() vs subscribe(onNext:) ☞ Where to switch to MainThread ? © Toshihiro Suzuki 2017 6
  7. share() vs shareReplay() vs shareReplayLatestWhileConnect ed() ☞ share() ... .publish().refCount()

    ☞ shareReplay(n) ... share() + replay(n) ☞ shareReplayLatestWhileConnected() ... shareReplay(1)ͱ΄΅Ұ ॹ Ref: https://www.slideshare.net/yukitakahashi3139241/hot-cold © Toshihiro Suzuki 2017 7
  8. Demo © Toshihiro Suzuki 2017 8

  9. combineLatest vs withLatestFrom ☞ combineLatest ͸fst snd ྆ํ͕τϦΨʹͳΔ (྆ํ_latest஋ΛΩϟογϡ) ☞

    withLatestFrom ͸fst ͚͕ͩτϦΨʹͳΔ (ͦͷ෼fstͷΩϟογϡ͕ෆཁ) ☞ withLatestFrom ͰࡁΉͷͰ͋Ε͹ɺcombineLatestΑΓcheap © Toshihiro Suzuki 2017 9
  10. .of() vs .just() / .empty() vs .never() ☞ flatMapͷதͰ͸ .just()

    ? ☞ ฒ΂͍ͨͱ͖͸ .of() ? Observable .of(tap, isReachable.filter { $0 == true }.map(void)) .merge() .flatMap { () -> Observable<ApiRequest> in guard let req = Api.createRequest() else { return .empty() // .never() Ͱ΋ಈ͘(ΑͶʁ) } return .just(req) // .of(req) Ͱ΋ಈ͘ } © Toshihiro Suzuki 2017 10
  11. Observable vs Variable vs Driver vs Subject vs ... class

    ViewModel { let label = Driver<String>.never() let label = Variable<String>("") let label = PublishSubject<String>() let label: Observable<String> } © Toshihiro Suzuki 2017 11
  12. do(onNext:) vs bindNext() vs bindTo() vs subscribe(onNext:) viewModel.rx.label.observeOn(MainScheduler.instance) .do(onNext: {

    [weak self] text in self?.imageView.isHidden = true }) .bindTo(label.rx.text) // ΤϥʔདྷͯͨΒfatalError (DEBUG) .addDisposableTo(rx_disposeBag) © Toshihiro Suzuki 2017 12
  13. do(onNext:) vs bindNext() vs bindTo() vs subscribe(onNext:) viewModel.rx.label.observeOn(MainScheduler.instance) .do(onNext: {

    [weak self] text in self?.imageView.isHidden = true }) .subscribe(onNext: { [weak self] text in // Τϥʔདྷͯͨͱͯ͠΋ແࢹ self?.label.text = text self?.label.isHidden = false }) .addDisposableTo(rx_disposeBag) © Toshihiro Suzuki 2017 13
  14. do(onNext:) vs bindNext() vs bindTo() vs subscribe(onNext:) viewModel.rx.label.observeOn(MainScheduler.instance) .do(onNext: {

    [weak self] text in self?.imageView.isHidden = true }) .bindNext { [weak self] text in // ΤϥʔདྷͯͨΒfatalError (DEBUG) self?.label.text = text self?.label.isHidden = false } .addDisposableTo(rx_disposeBag) © Toshihiro Suzuki 2017 14
  15. Where to switch to MainThread ? ☞ ViewModel૬౰? View? override

    func viewDidLoad() { super.viewDidLoad() viewModel.rx.label // ͜ͷlabel͸ϝΠϯεϨου͔ʁʁ // UIBindingObserver͸಺෦తʹmainʹ౤͛௚ͯ͘͠ΕΔ͔Β·͍͍͔͋ɻɻ .bindTo(label.rx.text) .addDisposableTo(rx_disposeBag) } © Toshihiro Suzuki 2017 15
  16. Where to switch to MainThread ? ☞ ViewModel૬౰? View? ☞

    ViewͰ΍Δํ͕໌ࣔతͰΘ͔Γ΍͍͢ͱ͍͏ҙݟ΋ override func viewDidLoad() { super.viewDidLoad() viewModel.rx.label .observeOn(MainScheduler.instance) // ͋ɺΦʔέʔΦʔέʔ .bindTo(label.rx.text) .addDisposableTo(rx_disposeBag) } © Toshihiro Suzuki 2017 16
  17. ·ͱΊ ☞ ಉ͡ॲཧͰ΋৭ʑͳॻ͖ํ͕Ͱ͖Δ ☞ ࣅ͍ͯΔΑ͏Ͱ΋಺෦ॲཧ͕ҟͳͬͨΓ͢Δ ☞ ͪΐͬͱͣͭཧղΛਂΊͯศརʹ࢖͍͖ͬͯ·͠ΐ͏! © Toshihiro Suzuki

    2017 17