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

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

0cf23fed58f84e729aa59ba5d8d0baaf?s=128

ukitaka

July 18, 2016
Tweet

Transcript

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

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

  3. None
  4. Hot / Cold

  5. Hot / Cold • Hot / Cold Ή͔͍ͣ͠ • Hot

    / Cold Α͘Θ͔Βͳ͍ • Hot / Cold ཧղͰ͖ͳ͍ • Rx೉ͦ͠͏… Rxා͍…
  6. ࡾᵆ( ^o^)ᵒ

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

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

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

    ͠·͢ɻ
  10. ͜ͷεϥΠυͰ͸ • ྫ (ΫΠζ) Λग़͠ͳ͕ΒHot / Cold ͦΕͧΕ ͕ͲΜͳੑ࣭Λ͍࣋ͬͯΔͷ͔֬ೝ͍͖ͯ͠ ·͢ɻ

    • ·ͩͦΕͧΕͷੑ࣭΍ҧ͍Λઆ໌͍ͯ͠ͳ͍ ͷͰ͕͢ɺ͋ͱͰઆ໌͠΍͘͢͢ΔͨΊʹ࠷ ॳʹʮલఏ஌ࣝʯͱʮݟ෼͚ํʯͷϧʔϧΛ આ໌͠·͢ɻ
  11. લఏ஌ࣝ

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

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

  14. ͡Ό͋ͳʹ͕Observable͔

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

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

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

    ← !? • map, filter, flatMap … • create, of, just, error …
  18. લఏ஌ࣝ ͦͷ2 ֤छOperator (Λද͢Ϋϥε) ͸ ʮObservableʯͰ͋Δ

  19. filter

  20. filter ฦΓ஋ͷܕ͸Observable

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

  22. just

  23. just ฦΓ஋ͷܕ͸Observable

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

  25. લఏ஌ࣝ·ͱΊ • Hot / Cold ͸ ʮObservableʯͷੑ࣭ • Subject͸ʮObservableʯ •

    Operator (Λද͢Ϋϥε) ͸ʮObservableʯ
  26. Hot / Cold ͷ ݟ෼͚ํ

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

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

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

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

    replayAll • connect / refCount • share / shareReplay • shareReplayLatestWhileConnected
  31. SubjectͳͷͰHot

  32. Map Operator͸Cold

  33. Filter Operator͸Cold

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

  35. ݟ෼͚ํ·ͱΊ • Subject͸ Hot • Operator͸ ΄ͱΜͲ͕ Cold • publishͳͲҰ෦ͷOperator͸

    Hot (ʹؔ࿈)
  36. ͰɺHot / Cold ͬͯͳʹʁ

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

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

  39. None
  40. Answer ਖ਼ղ͸…

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

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

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

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

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

  46. ͡Ό͋subscribeͨ͠Βʁ

  47. ͡Ό͋subscribeͨ͠Βʁ

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

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

  50. None
  51. Answer ਖ਼ղ͸…

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

  53. ղઆ Subject Map

  54. ղઆ Subject Map

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

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

  57. None
  58. Answer ਖ਼ղ͸…

  59. A. 1, 2, 3

  60. ղઆ Subject Map Hot

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

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

    ΛՔಇͤ͞Δ
  63. Hot ͷ΋͏Ұͭେ͖ͳੑ࣭

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

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

  66. ෼ذͰ͖Δͱ͸ʁ • ෼ذͰ͖Δ = ObserverΛෳ਺࣋ͯΔ • Cold ͳObservable͸ObserverΛ1͔ͭ࣋ͯ͠ͳ͍ • Hot

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

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

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

  70. Sequence Map

  71. Sequence Map Observer 1ͭΊ͕subscribe

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

  73. Sequence Map Observer Map Observer

  74. Sequence Map Observer Sequence Map Observer

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

  76. ͦͷ݁ՌͲ͏ͳΔ͔

  77. ͦͷ݁ՌͲ͏ͳΔ͔

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

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

  80. Hotͷ৔߹

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

  82. Hotͷ৔߹ Sequence Map Hot

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

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

  85. Hotͷ৔߹ Sequence Map Observer Hot Observer

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

  87. ͦͷ݁ՌͲ͏ͳΔ͔

  88. ͦͷ݁ՌͲ͏ͳΔ͔

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

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

  91. Hot ͷ࠷ޙͷੑ࣭

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

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

  94. Answer ਖ਼ղ͸…

  95. A. 2, 4, 6

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

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

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

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

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

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

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

  103. ղઆ

  104. ͡Ό͋Coldͷ৔߹͸ʁ

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

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

  107. ͡Ό͋Coldͷ৔߹͸ʁ

  108. ੑ࣭·ͱΊ

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

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

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

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

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

  114. Ҏ্ʂ

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