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

今日こそ理解するHot変換@社内RxSwift勉強会

 今日こそ理解するHot変換@社内RxSwift勉強会

今日こそ理解するHot変換

0cf23fed58f84e729aa59ba5d8d0baaf?s=128

ukitaka

July 18, 2016
Tweet

Transcript

  1. ࠓ೔ͦ͜ཧղ͢Δ Hotม׵ @ࣾ಺RxSwiftษڧձ

  2. https://speakerdeck.com/ukitaka/cold-at-she-nei-rxswiftmian-qiang-hui લճͷଓ͖

  3. Hotม׵

  4. Cold → Hot

  5. (෮श)Hotʹؔ࿈͢ΔΦϖϨʔλ Hotʹؔ࿈͢ΔΦϖϨʔλ͸ҎԼͷΈɻ ͜ΕΒ͕ಛʹݺ͹Εͯͳ͚Ε͹Hotʹม׵͞ΕΔ͜ͱ΋ͳ͍ɻ • multicast / publish • replay /

    replayAll • connect / refCount • share / shareReplay • shareReplayLatestWhileConnected
  6. ·͊ͱΓ͋͑ͣ࢖ͬͯΈΔ͔

  7. publish()

  8. Question1 ͳΜͱදࣔ͞ΕΔͰ͠ΐ͏ʁ

  9. None
  10. ͜Μͳͷ؆୯͡ΌΜʂ 1,2,3Ͱ͠ΐʂ

  11. ͜Μͳͷ؆୯͡ΌΜ Sequence Map Hot subscribe Քಇ

  12. ͜Μͳͷ؆୯͡ΌΜ Sequence Map Hot subscribe Քಇ

  13. Answer ਖ਼ղ͸…

  14. A. ͳʹ΋දࣔ͞Εͳ͍

  15. !?

  16. (෮श)Hotʹؔ࿈͢ΔΦϖϨʔλ Hotʹؔ࿈͢ΔΦϖϨʔλ͸ҎԼͷΈɻ ͜ΕΒ͕ಛʹݺ͹Εͯͳ͚Ε͹Hotʹม׵͞ΕΔ͜ͱ΋ͳ͍ɻ • multicast / publish • replay /

    replayAll • connect / refCount • share / shareReplay • shareReplayLatestWhileConnected ؚΈͷ͋Δݴ͍ํ
  17. Hotม׵ ϙΠϯτͦͷ1 ConnectableObservable

  18. ͭ͡͸publish()ͷฦΓ஋…

  19. ConnectableObservable • connect() ϝιουΛ࣋ͭ • connect() ΛݺͿ͜ͱͰHotʹม׵׬ྃ͢Δ • connect() ΛݺͿ·ͰObserver͕͍Α͏͕͍

    ·͍͕ಈ࡞͠ͳ͍
  20. ConnectableObservable

  21. ͳΜͰඞཁͳͷʁ • ྫ͑͹ɺෳ਺ͷobserver͕Hotม׵͞Εͨετ ϦʔϜͷ஋Λ࠷ॳ͔ΒͪΌΜͱड͚औΓ͍ͨ ࣌ɻconnect()ΛݺͿ·Ͱಈ࡞͠ͳ͍ͷͰɺͦ ͷؒʹશͯͷobserverΛsubscribe͓ͤͯ͘͞ ͜ͱ͕Ͱ͖Δɻ

  22. ͳΜͰඞཁͳͷʁ (ConnectableObservable͕ແ͍ͱͲ͏ͳΔ͔͸ߟ͑ͯΈ͍ͯͩ͘͞)

  23. ConnectableObservableΛฦ͢Operator • multicast() • publish() • replay(bufferSize:) • replayAll()

  24. multicast() • ͦ΋ͦ΋Hotม׵ͱ͸ɺ಺෦తʹSubjectΛར ༻͍ͯ͠Δ͚ͩͰ͋Δɻ • multicast()͸SubjectΛࢦఆͯͦ͜͠ͰҰ୴Π ϕϯτΛड͚ΔΑ͏ʹ͢ΔOperator

  25. multicast()

  26. publish() • PublishSubjectʹmulticast͢Δ

  27. • ReplaySubjectʹmulticast͢Δ replay(bufferSize:) / replayAll()

  28. ConnectableObservable·ͱΊ • ColdͳObservableʹରͯ͠ɺmulticast() / publish() / replay(bufferSize:) / replayAll() Λݺͼ

    ग़͢͜ͱͰ ConnectableObservableʹม׵Ͱ͖ Δɻ • ConnectableObservableʹରͯ͠connect()Λݺͼ ग़͢͜ͱͰHotͳObservableʹม׵͕׬ྃ͢Δɻ
  29. ࣍΁…

  30. ConnectableObservable ཧղͨ͠

  31. ଞͷHotؔ࿈ͷOperator͸ʁ

  32. (෮श)Hotʹؔ࿈͢ΔΦϖϨʔλ Hotʹؔ࿈͢ΔΦϖϨʔλ͸ҎԼͷΈɻ ͜ΕΒ͕ಛʹݺ͹Εͯͳ͚Ε͹Hotʹม׵͞ΕΔ͜ͱ΋ͳ͍ɻ • multicast / publish • replay /

    replayAll • connect / refCount • share / shareReplay • shareReplayLatestWhileConnected
  33. ͱΓ͋͑ͣshare()Ͱ΋ ࢖ͬͯΈΔ͔

  34. share() ConnectableObservable͡Όͳͯ͘ ObservableͰฦ͖ͬͯͯΔ͠ ͖ͬͱHot΍Ζʂʂʂ

  35. Question2 ͳΜͱදࣔ͞ΕΔͰ͠ΐ͏ʁ

  36. None
  37. Answer ਖ਼ղ͸…

  38. A. ͳʹ΋දࣔ͞Εͳ͍

  39. ƅЧƅ)Űō?

  40. Hotม׵ ϙΠϯτͦͷ2 ۂऀ: RefCount

  41. share()

  42. refCount() • refCount() ͸ ConnectableObservableʹର͠ ͯݺ΂Δ΋͏ҰͭͷOperatorͰ͋Δɻ

  43. refCount() • Hotͷ֓೦Λ෴͢ΊΜͲ͍͘͞ศརͳ΍ͭ • Hotͷੑ࣭ͱͯ͠ʮsubscribe͠ͳͯ͘΋ಈ࡞ ͢Δʯͱ͍͏ͷ͕͋Δ͕ɺrefCount͕ڬ·Δ ͱʮHot͕ͩɺ࠷ॳʹsubscribe͞ΕΔ·Ͱಈ ࡞͠ͳ͍ʯΈ͍ͨͳڍಈʹͳΔɻ

  44. refCount() • ࡞Γ͸୯७Ͱɺ࠷ॳʹsubscribe͞Εͨ࣌఺Ͱ ಺෦తʹconnect()ΛݺͿɻ

  45. Question3 ͳΜͱදࣔ͞ΕΔͰ͠ΐ͏ʁ

  46. None
  47. Answer ਖ਼ղ͸…

  48. A. 1,2,3

  49. ͪΌΜͱHotม׵͞Εͯͨ (ͳΜͰʁʂͱࢥͬͨํ͸લճͷεϥΠυ΋ΈͯΈ·͠ΐ͏)

  50. ࢒ΓͷOperator

  51. shareReplay(bufferSize:) • ໊લͷ௨Γɺshare + replay • share = publish +

    refCount • shareReplay = publish + refCount + replay • ʮHot͕ͩɺ࠷ॳʹsubscribe͞ΕΔ·Ͱಈ࡞͠ͳ ͍ʯΛकΓͭͭࢦఆ͞Εͨݸ਺͚ͩ஋Λreplay͢ Δ
  52. shareReplay(bufferSize:) • ࣮͸ΊͬͪΌॏཁͰɺRxSwiftͷExampleͰ΋Α͘ʢʁʣ࢖ ΘΕ͍ͯΔɻ • ม਺తʹѻ͍ͭͭɺ࠶ܭࢉͤ͞ͳ͍Α͏ʹ͢Δͱ͖ʹ shareReplay(1)Λ͚ͭΔͱΑ͍ (͜Εʹ͍ͭͯ͸ผ్ൃද͠ ·͢ɻ) •

    RxSwift v2.5.0࣌఺Ͱ͸shareReplayʹ1Ҏ֎Λ౉͢ͱ࠶ܭࢉ ͕૸Δόάʁ࢓༷ʁ͕͋Δɻshare΋ಉ༷ͷݱ৅༗Γɻ
  53. shareReplayLatestWhileConnected() • ໊લ͕ΊͬͪΌ௕͍ • ͜Μͳͷ࢖͏ػձ͋Δͷʁͱࢥ͏͔΋͠Εͳ͍͕ɺ RxSwift(RxCocoa)Ͱఏڙ͞Ε͍ͯΔ Driver͸σϑΥϧτͰ͜Εͱಉ ͡ڍಈΛ͢ΔɻͳͷͰڍಈ͸ཧղ͓ͯ͘͠΂͖ɻ • ShareReplay1

    ͱͷҧ͍͸ɺconnect தͰͳ͍ͱ࠷ޙͷ஋͕औΕͳ͍ ͜ͱɻͭ·Γ1౓ .Completed ͕ྲྀΕͨΒͦͷ͋ͱʹ͸replay͞Ε ͣɺ.Completed ͔͠ྲྀΕͯ͜ͳ͍ɻ • ڍಈͷৄࡉͳྫ͸Driverͷͱ͖ʹઆ໌͢ΔͷͰ͜͜Ͱ͸ׂѪɻ
  54. Hotม׵·ͱΊ Hotʹม׵͢Δʹ͸ • ConnectableObservableʹม׵ޙɺconnect() ΛݺͿ • RefCountʹରͯ͠1ͭҎ্subscribeΛߦ͏

  55. Ҏ্ʂʂ