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

RxSwift コードリーディングの勘所@社内RxSwift勉強会

RxSwift コードリーディングの勘所@社内RxSwift勉強会

0cf23fed58f84e729aa59ba5d8d0baaf?s=128

ukitaka

July 18, 2016
Tweet

Transcript

  1. RxSwift ίʔυϦʔσΟϯάͷצॴ @ࣾ಺RxSwiftษڧձ

  2. ͳΜͰίʔυಡΉඞཁ͕͋Δͷʁ https://speakerdeck.com/bricklife/rxswiftfalseobserveontosubscribeonwoli-jie-suru

  3. ͳΜͰίʔυಡΉඞཁ͕͋Δͷʁ • ެࣜυΩϡϝϯτ΍ RxMarblesͳͲυΩϡϝϯτ ͸ॆ࣮͍ͯ͠ΔΑ͏ʹݟ͑Δ͕ɺHotม׵΍Ϛϧν εϨουͳͲ͕ؔ܎͖ͯͨ࣌͠ͷڍಈ͸υΩϡϝ ϯτΛ௥͏͚ͩͰ͸ཧղ͢Δͷ͸೉͍͠ɻ • ·ͨɺRxSwiftಠࣗͷ֓೦΍OperatorͳͲɺ ReactiveXͷυΩϡϝϯτʹ͸ॻ͔Ε͍ͯͳ͍͜ͱ

    ΋গ͋͠Δɻ(DriverͳͲ…)
  4. ͜ͷεϥΠυͰ͸ • ओʹOperatorͷ࣮૷Ͱ޿͘࢖ΘΕ͍ͯΔ֓೦ ΍ख๏Λઆ໌͠·͢ɻ • ͦͷ͋ͱͰ Hot / Cold ͳͲΛྫʹ͋͛ͯɺͲ

    ͏΍࣮ͬͯݱ͞Ε͍ͯΔͷ͔Λઆ໌͠·͢ɻ
  5. ͜ͷεϥΠυͰ͸ • RxSwift͸ v2.5.0 (2016೥7݄࣌఺Ͱͷmaster) Λݟͯ·͢ɻ • ͨͿΜόʔδϣϯ1ܥ΍ࠓޙͷόʔδϣϯͰ΋ ௨༻͢Δ͔͸ෆ໌Ͱ͕͢ɺͨͿΜେ͖͘ζϨ Δ͜ͱ͸ແ͍ͱࢥ͍·͢ɻ

  6. צॴ ͦͷ1 Operatorʹ͓͚Δ ʮsourceʯ

  7. None
  8. Sequence Map Filter ↑ Α͘Έ͔͚Δਤ

  9. Sequence Map Filter ↑ Α͘Έ͔͚Δਤ ίʔυ্͸Ͳ͏දݱ͞Ε͍ͯΔʁ

  10. filter

  11. filter ࣮ଶ͸Filterͱ͍͏Ϋϥε

  12. filter sourceͱͯ͠ɺfilterΛݺͼग़ͨ͠ ObservableΛ͍࣋ͬͯΔ

  13. map Map΋ಉ༷

  14. ͭ·Γ source Mapͷsource͸Sequence (Sequence͸Observable.ofͷ಺෦දݱ)

  15. ͭ·Γ source Filterͷsource͸Map

  16. ͭ·Γ source Sequence Map Filter source

  17. ͭ·Γ source Sequence Map Filter source sourceͷ࿈ͳΓͱ΋ݴ͑Δ

  18. ͱ͜ΖͰɺ source Sequence Map Filter source ͜Εͷsource͸ʁ

  19. createܥͷOperatorʹ͸source͸ͳ͍ • create, just, of, error, never ͳͲɺcreateܥͷ Operatorʹ͸source͸ͳ͍ɻ •

    map΍filterͷΑ͏ͳҰൠతͳOperator΍ɺ Hotม׵ܥɺsubscribeOn/observeOnʹ͸ source͕͋Δɻ
  20. createܥͷOperatorʹ͸source͸ͳ͍ • ͔͜͜Β͸ɺී௨ʹʮOperatorʯͱॻ͍͍ͯ ͨΒʮcreateܥҎ֎ͷOperatorʯͷ͜ͱͩͱ ࢥ͍ͬͯͩ͘͞ɻ

  21. source͸Ͳ͏࢖ΘΕΔͷʁ • source͸ཁ͸ObservableͳͷͰɺద౰ͳλΠ ϛϯάͰͦͷsourceΛ(಺෦తʹ) subscribe͠ ͯΠϕϯτΛड͚औΓ࢝Ί·͢ɻ • ͲͷλΠϛϯάͰ(಺෦తʹ)subscribe͕ݺ͹ ΕΔ͔͸ɺHot /

    ColdͳͲOperatorͷੑ࣭ʹ Αͬͯҧ͖ͬͯ·͢ɻ
  22. sourceʹ͍ͭͯ·ͱΊ • createܥҎ֎ͷOperator͸લͷObservableΛ sourceͱͯ࣋ͭ͜͠ͱͰɺਤͷΑ͏ͳ࿈ͳΓ Λ࣮ݱ͍ͯ͠Δɻ source Sequence Map Filter source

  23. צॴ ͦͷ2 Operator࣮૷Ͱ νΣοΫ͢΂͖఺

  24. Operatorͷ࣮૷ͰͳʹΛΈΔ͔ ҎԼ͸ͲͷOperatorʹ΋ڞ௨͢Δಈ͖ • ద౰ͳλΠϛϯάͰsourceΛsubscribe͢Δ • source͔Βड͚औͬͨΠϕϯτʹద౰ͳॲཧ ΛՃ͑ͯ࣍ʹྲྀ͢

  25. Operatorͷ࣮૷ͰͳʹΛΈΔ͔ Sequence Map Filter Observer subscribe

  26. Operatorͷ࣮૷ͰͳʹΛΈΔ͔ subscribe Sequence Map Filter Observer subscribe subscribe ಺෦తʹsourceΛsubscribe͢Δ

  27. Operatorͷ࣮૷ͰͳʹΛΈΔ͔ Sequence Map Filter Observer on on on ΠϕϯτΛड͚औͬͯॲཧΛՃ͑ͯ࣍ʹྲྀ͢

  28. Operatorͷ࣮૷ͰͳʹΛΈΔ͔ subscribe Sequence Map Filter Observer subscribe subscribe on on

    on
  29. Operatorͷ࣮૷ͰͳʹΛΈΔ͔ ΈΔϙΠϯτ͸3ͭ • ͍ͭsourceͷsubscribeΛߦ͏͔ • subscribe͕ݺ͹ΕͨΒͳʹ͕ى͜Δ͔ • on͕ݺ͹ΕͨΒͳʹ͕ى͜Δ͔

  30. ྫ: Filter

  31. ྫ: Filter run͸subscribe͕ݺ͹Εͨͱ͖ʹݺ͹ΕΔϝιου

  32. ྫ: Filter sinkͱ͔͍͏΍͕ͭsourceͷsubscribeΛߦ͍ͬͯΔ

  33. ྫ: Filter Sequence Map Filter Observer subscribe ͭ·Γɺࣗ਎ͷsubscribe͕ݺ͹ΕͨΒ

  34. ྫ: Filter Sequence Map Filter Observer subscribe subscribe sourceͷsubscribeΛߦ͏

  35. ྫ: Filter

  36. ྫ: Filter on͸source͔ΒΠϕϯτ͕ྲྀΕ͖ͯͨ࣌ʹݺ͹ΕΔϝιου

  37. ྫ: Filter Nextͷ৔߹͸

  38. ྫ: Filter ൑ఆ༻ͷؔ਺ʹ஋Λ౉ͯ͠

  39. ྫ: Filter ৚݅Λຬ͍ͨͯ͠Ε͹࣍΁ྲྀ͢

  40. ྫ: Filter Sequence Map Filter Observer on source͔ΒΠϕϯτ͕དྷͨΒ

  41. ྫ: Filter Sequence Map Filter Observer on on ൑ఆͨ͠ޙ࣍΁౉͢

  42. ಡΊΔʂ ✌('ω'✌ )ࡾ✌('ω')✌ࡾ( ✌'ω')✌

  43. ·ͱΊ ΈΔϙΠϯτ͸3ͭ • ͍ͭsourceͷsubscribeΛߦ͏͔ • subscribe͕ݺ͹ΕͨΒͳʹ͕ى͜Δ͔ • on͕ݺ͹ΕͨΒͳʹ͕ى͜Δ͔

  44. Subject͸… ಉ͘͡3ͭΛΈΔͱڍಈ͕͔ͭΊΔͱࢥ͍·͢ɻ • ͍ͭsourceͷsubscribeΛߦ͏͔ • subscribe͕ݺ͹ΕͨΒͳʹ͕ى͜Δ͔ • on͕ݺ͹ΕͨΒͳʹ͕ى͜Δ͔

  45. createܥͷOperator͸… ͱΓ͋͑ͣɺ • subscribe͕ݺ͹ΕͨΒͳʹ͕ى͜Δ͔ ͜ΕΛݟ͓͚ͯ͹ڍಈ͕Θ͔Δͱࢥ͍·͢ɻ

  46. צॴ ͦͷ3 Sink / SinkIter

  47. (෮श) Operatorͷಈ͖ • ద౰ͳλΠϛϯάͰsourceΛsubscribe͢Δ • source͔Βड͚औͬͨΠϕϯτʹద౰ͳॲཧ ΛՃ͑ͯ࣍ʹྲྀ͢

  48. (෮श) Operatorͷಈ͖ • ద౰ͳλΠϛϯάͰsourceΛsubscribe͢Δ • source͔Βड͚औͬͨΠϕϯτʹద౰ͳॲཧ ΛՃ͑ͯ࣍ʹྲྀ͢ ୭͕subscribe͢Δͷʁobserver͸୭ʁ

  49. Filter

  50. Filter

  51. Filter

  52. Sink

  53. Sinkͱ͸ʁ • (಺෦తʹ)sourceͷobserverʹͳΔ΍ͭ • Filterʹ͸FilterSinkɺMapʹ͸MapSinkɺͷΑ ͏ʹ΄΅શͯͷOperatorʹରͯ̋̋͠Sink͕ ଘࡏ͢Δ

  54. ಺෦తͳಈ͖ (subscribe࣌)

  55. Sequence Map Filter Observer

  56. Filter Sequence Map Filter Observer subscribe͕ݺ͹ΕͨΒrun͕ݺ͹ΕΔ subscribe(observer)

  57. Filter Sequence Map Filter Observer subscribe(observer) observer͕ͦͷ··౉͞ΕΔ

  58. Filter Sequence Map Filter Observer observerΛݩʹFilterSink͕࡞ΒΕΔ FilterSink(observer)

  59. Filter Sequence Map Filter Observer FilterSink͕sourceΛsubscribe͢Δ source.subscribe(FilterSink(observer))

  60. Map Sequence Map Filter Observer source.subscribe(FilterSink(observer))

  61. Map Sequence Map Filter Observer source.subscribe(FilterSink(observer)) observerͱͯ͠ɺFilterSink(observer)͕౉ͬͯ͘Δɻ

  62. Map Sequence Map Filter Observer MapSink(FilterSink(observer)) observerΛݩʹFilterSink͕࡞ΒΕΔ

  63. Map Sequence Map Filter Observer source.subscribe(MapSink(FilterSink(observer))) MapSink͕sourceΛsubscribe͢Δ

  64. ಺෦తͳಈ͖ (on࣌)

  65. MapSink Sequence Map Filter Observer Πϕϯτ͕ૹग़͞ΕΔ MapSink(FilterSink(observer)).on(.Next(2))

  66. MapSink Sequence Map Filter Observer Πϕϯτ͕selectorʹΑͬͯม׵͞ΕΔ .Next(2) → .Next(4)

  67. MapSink Sequence Map Filter Observer Πϕϯτ͕ૹग़͞ΕΔ FilterSink(observer).on(.Next(4))

  68. FilterSink Sequence Map Filter Observer Πϕϯτ͕ૹग़͞ΕΔ FilterSink(observer).on(.Next(4))

  69. FilterSink Sequence Map Filter Observer ৚݅νΣοΫ͞ΕΔ value % 4 ==

    0
  70. FilterSink Sequence Map Filter Observer Πϕϯτ͕ૹग़͞ΕΔ observer.on(.Next(4))

  71. ಡΊΔʂ ✌('ω'✌ )ࡾ✌('ω')✌ࡾ( ✌'ω')✌

  72. ·ͱΊ subscribe(MapSink(FilterSink(observer))) Sequence Map Filter Observer subscribe(FilterSink(observer)) subscribe(observer) observerΛSinkʹแΈͳ͕Β sourceΛsubscribe

  73. ·ͱΊ Sequence Map Filter Observer observer.on(.Next) FilterSink(observer).on(.Next) MapSink(FilterSink(observer)).on(.Next) SinkͰॲཧΛՃ͑ͳ͕Β observerʹΠϕϯτΛྲྀ͢

  74. SinkIter

  75. SinkIterͱ͸ʁ • merge΍flatMapͳͲɺ(গͳ͘ͱ΋಺෦తʹ) Observable<Observable<E>> Λѻ͏ͱ͖ʹ ར༻͞ΕΔɻ

  76. SinkIterͱ͸ʁ Observable<Observable<E>>

  77. SinkIterͱ͸ʁ Observable<Observable<E>> ֎ଆͷObservableΛ subscribe͢Δͷ͕Sink

  78. SinkIterͱ͸ʁ Observable<Observable<E>> ಺ଆͷObservableΛ subscribe͢Δͷ͕SinkIter

  79. ಡΜͰΈΑ͏ ColdͳObservable

  80. ColdͳObservableͷੑ࣭ • ͍͔ͭ͋͘Γ·͕͢ɺࠓճ͸ʮsubscribe͠ͳ ͍ͱಈ࡞͠ͳ͍ʯͱ͍͏ੑ࣭͕ίʔυϨϕϧ ͰͲ͏࣮ݱ͞Ε͍ͯΔ͔Λ֬ೝͯ͠Έ·͢ɻ • ݴ͍͔͑Ε͹ʮsubscribe͞Εͨͱ͖ʹಈ࡞Λ ࢝ΊΔʯͰ͢ɻ

  81. Just

  82. Just subscribe͞ΕΔͱ….

  83. Just ஋͕ૹग़͞ΕΔʂʂʂ

  84. Cold

  85. Cold subscribe Just Map Filter Observer subscribe subscribe on on

    on
  86. ಡΜͰΈΑ͏ BehaviorSubject

  87. BehaviorSubjectͷੑ࣭ • ͍͔ͭ͋͘Γ·͕͢ɺࠓճ͸ʮ෼ذͰ͖Δʯ ʮsubscribe࣌ʹ௚લͷ஋͕औΕΔʯΛ֬ೝ͠ ͯΈ·͢ɻ

  88. BehaviorSubject ௚લͷ஋Λอ࣋͢Δม਺

  89. BehaviorSubject ෳ਺ͷobserver

  90. BehaviorSubject subscribe͞ΕͨΒݺ͹ΕΔ

  91. BehaviorSubject subscribe͞ΕͨΒobserverͱͯ͠ొ࿥

  92. BehaviorSubject ௚લͷ஋Λૹग़

  93. BehaviorSubject onͰΠϕϯτΛड͚औͬͨͱ͖ʹݺ͹ΕΔ

  94. BehaviorSubject ௚લͷ஋Λߋ৽

  95. BehaviorSubject ֤Observerʹૹग़

  96. ಡΊΔʂ ✌('ω'✌ )ࡾ✌('ω')✌ࡾ( ✌'ω')✌

  97. ·ͱΊ • Rxʹ͸ଟ͘ͷOperator͕༻ҙ͞Ε͍ͯ·͢ ͕ɺͦΕΒ͸΄΅ಉ͡࡞Γʹͳ͓ͬͯΓɺಡ Έํ͑͞෼͔Ε͹؆୯ʹڍಈΛ֬ೝ͢Δ͜ͱ ͕Ͱ͖·͢ɻ • ڍಈʹࣗ਎͕ͳ͚Ε͹ιʔεΛಡΜͰ֬ೝ͠ Α͏ʂ

  98. Ҏ্ʂ