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

toshi0383

April 15, 2017
Tweet

More Decks by toshi0383

Other Decks in Programming

Transcript

  1. ͓͠ͳ͕͖ ☞ 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
  2. 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
  3. combineLatest vs withLatestFrom ☞ combineLatest ͸fst snd ྆ํ͕τϦΨʹͳΔ (྆ํ_latest஋ΛΩϟογϡ) ☞

    withLatestFrom ͸fst ͚͕ͩτϦΨʹͳΔ (ͦͷ෼fstͷΩϟογϡ͕ෆཁ) ☞ withLatestFrom ͰࡁΉͷͰ͋Ε͹ɺcombineLatestΑΓcheap © Toshihiro Suzuki 2017 9
  4. .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
  5. 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
  6. 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
  7. 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
  8. 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
  9. 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
  10. 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