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

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

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

ukitaka

July 18, 2016
Tweet

More Decks by ukitaka

Other Decks in Programming

Transcript

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

    View full-size slide

  2. RxͰΈͳ࠷ॳʹͭ·ͮ͘
    ϙΠϯτ

    View full-size slide

  3. Hot / Cold
    • Hot / Cold Ή͔͍ͣ͠
    • Hot / Cold Α͘Θ͔Βͳ͍
    • Hot / Cold ཧղͰ͖ͳ͍
    • Rx೉ͦ͠͏… Rxා͍…

    View full-size slide

  4. ࡾᵆ( ^o^)ᵒ

    View full-size slide

  5. ࡾᵆ( ^o^)ᵒ
    ※ Bond͸ྑ͍ϥΠϒϥϦͰ͢

    View full-size slide

  6. ͜ͷεϥΠυͰ͸
    • جຊతͳ֓೦Λཧղ্ͨ͠ͰɺHot / Cold Ͱ
    ৺͕ંΕͦ͏ʹͳͬͨํΛλʔήοτʹͯ͠
    ΔͷͰɺ͋Δఔ౓ͷRxSwiftͷཧղΛඞཁͱ͠
    ·͢ɻ

    View full-size slide

  7. ͜ͷεϥΠυͰ͸
    • ·ͨHotม׵ʹ͍ͭͯ͸௕͘ͳΔͷͰผͷػձ
    ʹઆ໌͠·͢ɻ
    • ͱΓ͋͑ͣ Hot / Cold ͷੑ࣭ʹ஫໨ͯ͠આ໌
    ͠·͢ɻ

    View full-size slide

  8. ͜ͷεϥΠυͰ͸
    • ྫ (ΫΠζ) Λग़͠ͳ͕ΒHot / Cold ͦΕͧΕ
    ͕ͲΜͳੑ࣭Λ͍࣋ͬͯΔͷ͔֬ೝ͍͖ͯ͠
    ·͢ɻ
    • ·ͩͦΕͧΕͷੑ࣭΍ҧ͍Λઆ໌͍ͯ͠ͳ͍
    ͷͰ͕͢ɺ͋ͱͰઆ໌͠΍͘͢͢ΔͨΊʹ࠷
    ॳʹʮલఏ஌ࣝʯͱʮݟ෼͚ํʯͷϧʔϧΛ
    આ໌͠·͢ɻ

    View full-size slide

  9. લఏ஌ࣝ

    View full-size slide

  10. લఏ஌ࣝ ͦͷ1
    Hot / Cold ͸
    ʮObservableʯͷੑ࣭

    View full-size slide

  11. ͳʹΛ౰ͨΓલͷࣄΛ
    ݴ͍ͬͯΔΜͩίΠπ

    View full-size slide

  12. ͡Ό͋ͳʹ͕Observable͔

    View full-size slide

  13. ͡Ό͋ͳʹ͕Observable͔
    • Subject
    • PublishSubject, BehaviorSubject, ReplaySubject …

    View full-size slide

  14. ͡Ό͋ͳʹ͕Observable͔
    • Subject
    • PublishSubject, BehaviorSubject, ReplaySubject …
    • Operator
    • map, filter, flatMap …
    • create, of, just, error …

    View full-size slide

  15. ͡Ό͋ͳʹ͕Observable͔
    • Subject
    • PublishSubject, BehaviorSubject, ReplaySubject …
    • Operator ← !?
    • map, filter, flatMap …
    • create, of, just, error …

    View full-size slide

  16. લఏ஌ࣝ ͦͷ2
    ֤छOperator (Λද͢Ϋϥε)
    ͸
    ʮObservableʯͰ͋Δ

    View full-size slide

  17. filter
    ฦΓ஋ͷܕ͸Observable

    View full-size slide

  18. filter
    ࣮ଶ͸filter operatorΛද͢
    Filterͱ͍͏Ϋϥε

    View full-size slide

  19. just
    ฦΓ஋ͷܕ͸Observable

    View full-size slide

  20. just
    ࣮ଶ͸Justͱ͍͏Ϋϥε

    View full-size slide

  21. લఏ஌ࣝ·ͱΊ
    • Hot / Cold ͸ ʮObservableʯͷੑ࣭
    • Subject͸ʮObservableʯ
    • Operator (Λද͢Ϋϥε)
    ͸ʮObservableʯ

    View full-size slide

  22. Hot / Cold ͷ ݟ෼͚ํ

    View full-size slide

  23. ݟ෼͚ํ ͦͷ1
    Subject͸HotͰ͋Δ

    View full-size slide

  24. Subject
    • PublishSubject
    • BehaviorSubject
    • ReplaySubject
    ͜ΕΒ͸HotͰ͋Δ

    View full-size slide

  25. ݟ෼͚ํ ͦͷ2
    ΄ͱΜͲͷOperator͸
    ColdͰ͋Δ

    View full-size slide

  26. Hotʹؔ࿈͢ΔΦϖϨʔλ
    Hotʹؔ࿈͢ΔΦϖϨʔλ͸ҎԼͷΈɻ
    ͜ΕΒ͕ಛʹݺ͹Εͯͳ͚Ε͹Hotʹม׵͞ΕΔ͜ͱ΋ͳ͍ɻ
    • multicast / publish
    • replay / replayAll
    • connect / refCount
    • share / shareReplay
    • shareReplayLatestWhileConnected

    View full-size slide

  27. SubjectͳͷͰHot

    View full-size slide

  28. Map Operator͸Cold

    View full-size slide

  29. Filter Operator͸Cold

    View full-size slide

  30. Subject Map Filter
    ↑ Α͘Έ͔͚Δਤ

    View full-size slide

  31. ݟ෼͚ํ·ͱΊ
    • Subject͸ Hot
    • Operator͸ ΄ͱΜͲ͕ Cold
    • publishͳͲҰ෦ͷOperator͸ Hot (ʹؔ࿈)

    View full-size slide

  32. ͰɺHot / Cold ͬͯͳʹʁ

    View full-size slide

  33. ͓·ͨͤ͠·ͨ͠
    Hot / Coldͷੑ࣭

    View full-size slide

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

    View full-size slide

  35. Answer
    ਖ਼ղ͸…

    View full-size slide

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

    View full-size slide

  37. Coldͷੑ࣭ ͦͷ1
    ColdͳObservable͸
    subscribe͞ΕΔ·Ͱ
    ಈ࡞͠ͳ͍

    View full-size slide

  38. Cold͸subscribe͞ΕΔ·Ͱಈ࡞͠ͳ͍
    Sequence

    View full-size slide

  39. Cold͸subscribe͞ΕΔ·Ͱಈ࡞͠ͳ͍
    Sequence Map

    View full-size slide

  40. Cold͸subscribe͞ΕΔ·Ͱಈ࡞͠ͳ͍
    ୭΋subscribe͍ͯ͠ͳ͍ → ಈ࡞͠ͳ͍

    View full-size slide

  41. ͡Ό͋subscribeͨ͠Βʁ

    View full-size slide

  42. ͡Ό͋subscribeͨ͠Βʁ

    View full-size slide

  43. Hotͷੑ࣭ ͦͷ1
    HotͳObservable͸
    subscribe͞Εͳͯ͘΋
    ಈ࡞͢Δ
    ٯʹ

    View full-size slide

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

    View full-size slide

  45. Answer
    ਖ਼ղ͸…

    View full-size slide

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

    View full-size slide

  47. ղઆ
    Subject Map

    View full-size slide

  48. ղઆ
    Subject Map

    View full-size slide

  49. • ಈ࡞͠ͳ͍ = ड͚औΔ͜ͱ͢Β͠ͳ͍
    Cold͸subscribe͞ΕΔ·Ͱಈ࡞͠ͳ͍
    Subject Map
    Subject͸஋Λྲྀ͢ ड͚औΒͳ͍

    View full-size slide

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

    View full-size slide

  51. Answer
    ਖ਼ղ͸…

    View full-size slide

  52. ղઆ
    Subject Map Hot

    View full-size slide

  53. Hot͸લͷColdΛՔಇͤ͞Δ
    ͱॻ͘ͱ೉ͦ͠͏͕ͩɺཁ͸
    HotͳObservable͸தͰsubscribeΛݺΜͰΔ͚ͩɻ
    Subject Map Hot
    subscribe
    Քಇ

    View full-size slide

  54. ͜͜·Ͱͷ·ͱΊ
    • Cold ͳObservable͸subscribe͞ΕΔ·Ͱಈ࡞͠ͳ͍
    • Hot ͳObservable͸subscribe͞Εͳͯ͘΋ಈ࡞͢Δ
    • Hot ͳObservable͸લͷCold ΛՔಇͤ͞Δ

    View full-size slide

  55. Hot ͷ΋͏Ұͭେ͖ͳੑ࣭

    View full-size slide

  56. Hotͷੑ࣭ ͦͷ2
    HotͳObservable͸
    ෼ذͰ͖Δ

    View full-size slide

  57. Coldͷੑ࣭ ͦͷ2
    ColdͳObservable͸
    ෼ذͰ͖ͳ͍
    ٯʹ

    View full-size slide

  58. ෼ذͰ͖Δͱ͸ʁ
    • ෼ذͰ͖Δ = ObserverΛෳ਺࣋ͯΔ
    • Cold ͳObservable͸ObserverΛ1͔ͭ࣋ͯ͠ͳ͍
    • Hot ͳObservable͸ObserverΛෳ਺࣋ͯΔ
    ※ આ໌؆ུԽͷͨΊʹʮObserverʯͱ͍ͯ͠·͕͢ɺ΋ͪΖΜ͜͜·
    ͰͷྫͷΑ͏ʹ෼ذޙʹObservableΛͭͳ͛ͯߦ͘͜ͱ΋Ͱ͖·͢ɻ

    View full-size slide

  59. ͑ʁෳ਺࣋ͯͳ͍ͷʁ

    View full-size slide

  60. ͑ʁෳ਺࣋ͯͳ͍ͷʁ

    View full-size slide

  61. ͑ʁෳ਺࣋ͯͳ͍ͷʁ
    ͳΜ͔Ͱ͖ͦ͏ͳงғؾ͋Δ͚Ͳʁʁʁ

    View full-size slide

  62. Sequence Map

    View full-size slide

  63. Sequence Map Observer
    1ͭΊ͕subscribe

    View full-size slide

  64. Sequence Map Observer
    Map Observer
    2ͭΊ΋subscribe͍͚ͨ͠Ͳ
    ͢Ͱʹ1ͭ͞ΕͯΔ…

    View full-size slide

  65. Sequence Map Observer
    Map Observer

    View full-size slide

  66. Sequence Map Observer
    Sequence Map Observer

    View full-size slide

  67. Sequence Map Observer
    Sequence Map Observer
    ετϦʔϜ͕2ຊʹ !!

    View full-size slide

  68. ͦͷ݁ՌͲ͏ͳΔ͔

    View full-size slide

  69. ͦͷ݁ՌͲ͏ͳΔ͔

    View full-size slide

  70. ͦͷ݁ՌͲ͏ͳΔ͔
    Sequence Map Observer
    Sequence Map Observer
    2ετϦʔϜ෼ग़ྗ͞ΕΔ

    View full-size slide

  71. Hotͷ৔߹
    Hotʹ͢Δ(·ͩಈ࡞͸͠ͳ͍)
    (ৄࡉ͸Hotม׵ͷͱ͖ʹઆ໌)

    View full-size slide

  72. Hotͷ৔߹
    ͜͜ͰετϦʔϜ։࢝
    (ৄࡉ͸Hotม׵ͷͱ͖ʹઆ໌)

    View full-size slide

  73. Hotͷ৔߹
    Sequence Map Hot

    View full-size slide

  74. Hotͷ৔߹
    Sequence Map Hot Observer
    1ͭΊ͕subscribe

    View full-size slide

  75. Hotͷ৔߹
    Sequence Map Hot Observer
    Observer
    2ͭΊ΋subscribe

    View full-size slide

  76. Hotͷ৔߹
    Sequence Map
    Observer
    Hot
    Observer

    View full-size slide

  77. Hotͷ৔߹
    Sequence Map
    Observer
    Hot
    Observer
    ෼ذ !!!

    View full-size slide

  78. ͦͷ݁ՌͲ͏ͳΔ͔

    View full-size slide

  79. ͦͷ݁ՌͲ͏ͳΔ͔

    View full-size slide

  80. ͦͷ݁ՌͲ͏ͳΔ͔
    Sequence Map
    Observer
    Hot
    Observer
    Ұճ෼͔͠print͞Εͳ͍

    View full-size slide

  81. ͜͜·Ͱͷ·ͱΊ
    • Cold ͳObservable͸෼ذͰ͖ͳ͍
    • Hot ͳObservable͸෼ذͰ͖Δ

    View full-size slide

  82. Hot ͷ࠷ޙͷੑ࣭

    View full-size slide

  83. Question4
    ͳΜͱදࣔ͞ΕΔͰ͠ΐ͏ʁ

    View full-size slide

  84. ͜͜ͰετϦʔϜ։࢝
    (ৄࡉ͸Hotม׵ͷͱ͖ʹઆ໌)
    Hotʹ͢Δ(·ͩಈ࡞͸͠ͳ͍)

    View full-size slide

  85. Answer
    ਖ਼ղ͸…

    View full-size slide

  86. ͋Εʁ 2ճsubscribeͯ͠Δͷʹʁ

    View full-size slide

  87. Hotͷੑ࣭ ͦͷ3
    HotͳObservableͷΠϕϯτ͸
    ֤ObserverͰڞ༗͞ΕΔ

    View full-size slide

  88. ͢ΔͱͲ͏ͳΔ͔
    • ֤Observerʹରͯ͠ಉ͡Πϕϯτ͕ૹग़͞ΕΔɻͭ·
    Γͦ͜·ͰͷCold͸Կ౓΋ݺ͹Εͳ͍ɻ(෼ذͷੑ࣭
    ͱ͜ΖͰ֬ೝࡁΈ)
    • ͢ͰʹྲྀΕͨΠϕϯτ(Next)͸ૹग़͞Εͳ͍ɻͭ·Γ
    ్த͔Βsubscribe͢Δͱ్த͔Β͔͠ड͚औΕͳ͍ɻ

    View full-size slide

  89. ղઆ
    ετϦʔϜՔಇ։࢝࣌఺Ͱ
    observer͸1ͭ

    View full-size slide

  90. ղઆ
    2,4,6 ͕දࣔ͞ΕΔ

    View full-size slide

  91. ղઆ
    ͜͜ʹࢸͬͨ࣌఺ͰશͯͷNext͸͢Ͱʹૹग़ࡁΈɻ
    ࠶౓ྲྀΕΔ͜ͱ͸ͳ͍ɻ

    View full-size slide

  92. ղઆ
    Αͬͯɺͳʹ΋දࣔ͞Εͳ͍

    View full-size slide

  93. ͡Ό͋Coldͷ৔߹͸ʁ

    View full-size slide

  94. ͡Ό͋Coldͷ৔߹͸ʁ
    Sequence Map Observer
    Sequence Map Observer

    View full-size slide

  95. ͡Ό͋Coldͷ৔߹͸ʁ
    ͢Ͱʹղઆͨ͠௨ΓɺετϦʔϜࣗମ͕2ͭʹͳΔɻ
    ͳͷͰɺ྆ํͷObserverͰ࠷ॳͷΠϕϯτ͔Β͢΂ͯ
    ड͚औΔ͜ͱ͕Ͱ͖Δɻ
    Sequence Map Observer
    Sequence Map Observer

    View full-size slide

  96. ͡Ό͋Coldͷ৔߹͸ʁ

    View full-size slide

  97. ੑ࣭·ͱΊ

    View full-size slide

  98. Coldͷੑ࣭ ͦͷ1
    ColdͳObservable͸
    subscribe͞ΕΔ·Ͱ
    ಈ࡞͠ͳ͍

    View full-size slide

  99. Hotͷੑ࣭ ͦͷ1
    HotͳObservable͸
    subscribe͞Εͳͯ͘΋
    ಈ࡞͢Δ
    ٯʹ

    View full-size slide

  100. Hotͷੑ࣭ ͦͷ2
    HotͳObservable͸
    ෼ذͰ͖Δ

    View full-size slide

  101. Coldͷੑ࣭ ͦͷ2
    ColdͳObservable͸
    ෼ذͰ͖ͳ͍
    ٯʹ

    View full-size slide

  102. Hotͷੑ࣭ ͦͷ3
    HotͳObservableͷΠϕϯτ͸
    ֤ObserverͰڞ༗͞ΕΔ

    View full-size slide

  103. ࠷ޙʹ
    • ͜͜·Ͱઆ໌ͨ͠ͷ͸͋͘·Ͱ΋֓೦తͳ࿩
    Ͱ͢ɻͲ͜Ͱͳʹ͕ى͍ͬͯ͜Δ͔ΛͪΌΜ
    ͱཧղ͢Δʹ͸ɺ΍͸Γ࣮૷Λ͕Μ͹ͬͯಡ
    ΜͰ͍ͩ͘͞ɻ(ίʔυϦʔσΟϯάͷצॴ͸
    ผ్ൃද͠·͢ɻ)
    • Hotม׵ʹ͍ͭͯ΋ผ్ղઆ͠·͢ɻ

    View full-size slide