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

RxSwift Scheduler

RxSwift Scheduler

0b26590a0a8b0f1da140ed5de9b68379?s=128

USAMI Kosuke

August 20, 2018
Tweet

Transcript

  1. RxSwiftͷεέδϡʔϥ USAMI Kosuke Fenrir Inc.

  2. RxSwift • ඇಉظΠϕϯτΛड͚औΔͨΊͷ࿮૊Έ • UI Πϕϯτड͚औΓ • Web API Ϩεϙϯεड͚औΓ

    • σʔλͷมԽͷ؂ࢹ
  3. ྫ hogeObservable .subscribe(onNext: { _ in // ΠϕϯτΛड͚औͬͨͱ͖ͷॲཧ })

  4. ඇಉظΠϕϯτͱεϨου • ඇಉظΠϕϯτʹಉظతͰͳ͍ʹผεϨουͰൃੜ͢Δ • Rx ͰεϨου͸ͲͷΑ͏ʹѻΘΕΔͷ͔ʁ

  5. εέδϡʔϥ • Rx ͰεϨουʹ౰ͨΔ֓೦ • ʢਖ਼֬ʹ͸εϨουͱ1ର1Ͱ͸ͳ͍͚Ͳɺ͜͜Ͱ͸লུʣ

  6. ༨ஊɿRx ͷࡾཁૉ • Observable • Operator • Scheduler

  7. εέδϡʔϥ

  8. Observable ͱ Observer • ΠϕϯτൃੜݩͱΠϕϯτॲཧ

  9. Scheduler • ͦΕͧΕʹεέδϡʔϥΛࢦఆͰ͖Δ

  10. observeOn • ΠϕϯτॲཧͷεέδϡʔϥΛม͑Δ

  11. ྫ hogeObservable .observeOn(MainScheduler.instance) .subscribe(onNext: { _ in // ϝΠϯεϨουͰಈ࡞͢Δ })

  12. subscribeOn • ΠϕϯτൃੜݩͷεέδϡʔϥΛม͑Δ

  13. ྫ hogeObservable .subscribeOn(hogeScheduler) .do(onNext: { _ in // hogeScheduler Ͱಈ࡞͢Δ

    } .observeOn(MainScheduler.instance) .subscribe(onNext: { _ in // ϝΠϯεϨουͰಈ࡞͢Δ })
  14. εϨου੍ޚ·ͱΊ • subscribeOn : Πϕϯτൃੜݩͷεέδϡʔϥࢦఆ • Web API ࣮ߦεϨουͷࢦఆ •

    σʔλϕʔεΞΫηεεϨουͷࢦఆ • observeOn : Πϕϯτॲཧͷεέδϡʔϥͷࢦఆ • UI ॲཧεϨουͷࢦఆ
  15. ஫ҙ఺ • subscribeOn Ͱ੾Γସ͑Ͱ͖ͳ͍৔߹͕͋Δ • ͦΕ͸ Observable ͷੑ࣭ʹΑΔ

  16. Hot ͱ Cold

  17. subscribeOn ͕ޮ͔ͳ͍৔߹ • Hot ͳ Observable ͷ৔߹͸੾Γସ͑Ͱ͖ͳ͍ • Cold ͳ

    Observable ͷ৔߹͸੾Γସ͑Ͱ͖Δ
  18. ྫɿ੾Γସ͑Ͱ͖ͳ͍έʔε let hogeRelay = BehaviorRelay(value: "A") let hogeThread = Thread()

    { sleep(1) hogeRelay.accept("B") sleep(1) hogeRelay.accept("C") } hogeThread.start()
  19. ྫɿ੾Γସ͑Ͱ͖ͳ͍έʔε hogeRelay .subscribeOn(MainScheduler.instance) .do(onNext: { _ in // hogeThread Ͱ

    accept ͨ͠΋ͷ͸ hogeThread Ͱಈ࡞ } .observeOn(MainScheduler.instance) .subscribe(onNext: { _ in // ϝΠϯεϨουͰಈ࡞͢Δ })
  20. Observable ͷ෼ྨ : Hot / Cold • Hot • subscribe

    ͠ͳͯ͘΋ετϦʔϜ͕ྲྀΕΔ • subscribeOn Ͱ੍ޚͰ͖ͳ͍ • Cold • subscribe ͨ͠ΒετϦʔϜ͕ྲྀΕग़͢ • subscribeOn Ͱ੍ޚͰ͖Δ
  21. ͲΕ͕ Hot ͰͲΕ͕ Cold ͔ʁ • Hot • Subject ΍

    Relay • Rx ͷ֎ଆ͔ΒετϦʔϜʹΠϕϯτΛૹΕΔ • Cold • ্هҎ֎͸͍͍ͩͨ Cold • Rx ͷ੍ޚԼʹ͋Δ
  22. ·ͱΊ

  23. ·ͱΊ • εέδϡʔϥ • ੾Γସ͑Δํ๏ • Hot ͱ Cold •

    Hot ͸੾Γସ͑Ͱ͖ͳ͍