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

マルチスレッドRxSwift @ 社内RxSwift勉強会

マルチスレッドRxSwift @ 社内RxSwift勉強会

マルチスレッドRxSwift @ 社内RxSwift勉強会

0cf23fed58f84e729aa59ba5d8d0baaf?s=128

ukitaka

July 19, 2016
Tweet

Transcript

  1. ϚϧνεϨουRxSwift @ࣾ಺RxSwiftษڧձ

  2. ߹ΘͤͯಡΈ͍ͨ https://https://speakerdeck.com/ukitaka/rxswift-kodorideingufalsekan-suo-at-she-nei-rxswiftmian-qiang-hui/bricklife/rxswiftfalseobserveontosubscribeonwoli-jie-suru

  3. ཁίʔυϦʔσΟϯά • ޙͰৄ͘͠આ໌͠·͕͢ɺʮͲͷεϨουͰ subscribe͕ߦΘΕ͔ͨʯ͕ͱͯ΋ॏཁʹͳ͖ͬͯ ·͢ɻ • ͜͜Ͱ͍͏subscribe͸಺෦తͳsubscribe΋ؚΈ ·͢ɻͭ·ΓʮOperator͕͍ͭͲͷεϨουͰ sourceΛsubscribe͢Δ͔ʯΛ೺Ѳ͠ͳ͍ͱɺڍಈ Λཧղ͢Δͷ͕೉͍͜͠ͱ΋͋Δ͔΋͠Ε·ͤΜɻ

  4. ͜ͷεϥΠυͰ͸ • ·ͣɺRxSwiftͱGCDͳͲͷ૊Έ߹ΘͤʹΑΔ ϚϧνεϨου؀ڥͰͷڍಈͷ֬ೝͱಈ࡞ϧʔ ϧͷઆ໌Λߦ͍·͢ɻ • ͦͷޙobserveOnɺsubscribeOnͷڍಈͷઆ ໌Λߦ͍·͢ɻ

  5. ͜ͷεϥΠυͰ͸ • ϚϧνεϨουͱ͍ͬͯ΋ɺଟ͘ͷiOS։ൃऀ͕ڵຯ͕͋Δͷ ͸ʮϝΠϯεϨουʯ͔ʮͦΕҎ֎͔ʯͩͱࢥ͍·͢ɻ • ͜ͷεϥΠυͰ͸ɺʮϝΠϯεϨουͰͳ͍εϨουʯΛ·ͱ ΊͯʮόοΫάϥ΢ϯυεϨουʯͱݺͿ͜ͱʹͯ͠ɺϝΠϯ εϨου͔όοΫάϥ΢ϯυεϨου͔ͷΈʹ஫໨͠·͢ɻ • ͭ·ΓόοΫάϥ΢ϯυεϨου͸ෳ਺͋Γɺ࣮ࡍʹͲͷεϨο

    υͰॲཧ͞ΕΔ͔͸ɺ(RxSwift͕಺෦తʹར༻͍ͯ͠Δ) GCD ΍NSOperationQueueʹґଘ͢Δ͜ͱʹ஫ҙ͍ͯͩ͘͠͞ɻ
  6. ͜ͷεϥΠυͰ͸ • RxSwift͸ v2.5.0 (2016೥7݄࣌఺Ͱͷmaster) Λݟͯ·͢ɻ • ͨͿΜόʔδϣϯ1ܥ΍ࠓޙͷόʔδϣϯͰ΋ ௨༻͢Δ͔͸ෆ໌Ͱ͕͢ɺͨͿΜେ͖͘ζϨ Δ͜ͱ͸ແ͍ͱࢥ͍·͢ɻ

  7. લฤ Observableͱ εϨου

  8. ಈ࡞ϧʔϧ

  9. ϧʔϧͦͷ1 ௨ৗͷColdͳOperator͸ subscribe͞ΕͨεϨουͰsubscribe on͞ΕͨεϨουͰonΛݺͿɻ

  10. ϧʔϧͦͷ1(ଓ͖) ಛʹSchedulerͷࢦఆͳͲ͕ͳ͍৔߹ɺ createܥOperator͸subscribeͨ͠ε ϨουͰonΛݺͿɻ

  11. ݴ༿͚ͩͩͱΑ͘Θ͔ΒΜ

  12. None
  13. Sequence Observer

  14. Sequence Observer subscribe ϝΠϯεϨου όοΫάϥ΢ϯυεϨου

  15. Sequence Observer subscribe ϝΠϯεϨου όοΫάϥ΢ϯυεϨου on

  16. Question1 Ͳ͜ͷεϨουͰಈ࡞͢ΔͰ͠ΐ͏ʁ

  17. None
  18. Map͸ͲͷεϨουͰಈ࡞͢ΔͰ͠ΐ͏ʁ

  19. Answer ਖ਼ղ͸…

  20. A. όοΫάϥ΢ϯυεϨου

  21. ղઆ Sequence Map Observer subscribe ϝΠϯεϨου όοΫάϥ΢ϯυεϨου

  22. ղઆ subscribe Sequence Map Observer subscribe ϝΠϯεϨου όοΫάϥ΢ϯυεϨου ݺ͹ΕͨεϨουͰ಺෦తʹsourceͷ subscribeΛߦ͏ɻ

  23. ղઆ subscribe Sequence Map Observer subscribe ϝΠϯεϨου όοΫάϥ΢ϯυεϨου subscribe͕ݺ͹ΕͨεϨουonΛݺͿ

  24. ղઆ subscribe Sequence Map Observer subscribe on ϝΠϯεϨου όοΫάϥ΢ϯυεϨου on

  25. Question2 Ͳ͜ͷεϨουͰಈ࡞͢ΔͰ͠ΐ͏ʁ

  26. None
  27. Map͸ͲͷεϨουͰಈ࡞͢ΔͰ͠ΐ͏ʁ

  28. Answer ਖ਼ղ͸…

  29. A. όοΫάϥ΢ϯυεϨου

  30. ղઆ Sequence Map Observer Publish

  31. ղઆ Sequence Map Observer Publish subscribe

  32. ղઆ Sequence Map Observer subscribe Publish subscribe

  33. ղઆ subscribe Sequence Map Observer subscribe Publish subscribe

  34. ղઆ subscribe Sequence Map Observer subscribe Publish subscribe subscribe͕ݺ͹ΕͨεϨουonΛݺͿ

  35. ղઆ subscribe Sequence Map Observer subscribe on on Publish subscribe

    on
  36. (ิ଍) ղઆ subscribe Sequence Map Observer subscribe on on Publish

    subscribe on ಛʹઆ໌ͯ͠ͳ͔͕ͬͨɺSubject΋ on͞ΕͨεϨουͰonΛݺͿ
  37. Question3 Ͳ͜ͷεϨουͰಈ࡞͢ΔͰ͠ΐ͏ʁ

  38. None
  39. Map͸ͲͷεϨουͰಈ࡞͢ΔͰ͠ΐ͏ʁ

  40. Answer ਖ਼ղ͸…

  41. A. όοΫάϥ΢ϯυεϨου

  42. ղઆ • refCount() ͸ʮHot͕ͩɺ࠷ॳʹsubscribe͞ ΕΔ·Ͱಈ࡞͠ͳ͍ʯͱ͍͏ڍಈɻͭ·Γ࠷ ॳʹsubscribe͕ݺ͹ΕͨεϨου͕ॏཁɻ • ࠓճ͸࠷ॳʹsubscribeΛݺΜͰ͍Δͷ͕όο Ϋάϥ΢ϯυεϨουͳͷͰόοΫάϥ΢ϯ υͰಈ࡞͢Δɻ

  43. (࠶ܝ) ϧʔϧͦͷ1 ௨ৗͷColdͳOperator͸ subscribe͞ΕͨεϨουͰsubscribe on͞ΕͨεϨουͰonΛݺͿɻ

  44. (࠶ܝ) ϧʔϧͦͷ1(ଓ͖) ಛʹSchedulerͷࢦఆͳͲ͕ͳ͍৔߹ɺ createܥOperator͸subscribeͨ͠ε ϨουͰonΛݺͿɻ

  45. ࣍ͷϧʔϧ΁…

  46. ϧʔϧͦͷ2 createܥͷOperatorʹScheduler͕ࢦ ఆ͞Ε͍ͯΔ৔߹ɺsubscribe͢Δͱͦ ͷScheduler্ͰonΛݺͿɻ ͭ·ΓɺͲͷεϨουͰsubscribe͞Ε ͔ͨ͸ؔ܎ͳ͍ɻ (௕͍)

  47. Scheduler • createܥͷOperatorʹ͸ɺSchedulerΛࢦఆͰ ͖Δ΋ͷ͕͋Δɻ

  48. Scheduler • Scheduler͕ࢦఆ͞Ε͍ͯΔ৔߹ʹͲ͏͍͏ڍ ಈʹͳΔ͔ɺͱ͍͏ͷ͕ࠓճͷϧʔϧ

  49. None
  50. Scheduler͕ࢦఆ͞Ε͍ͯΔ

  51. Sequence Observer subscribe ϝΠϯεϨου όοΫάϥ΢ϯυεϨου

  52. Sequence Observer subscribe ϝΠϯεϨου όοΫάϥ΢ϯυεϨου

  53. Sequence Observer subscribe ϝΠϯεϨου όοΫάϥ΢ϯυεϨου on

  54. ͭ·Γ • Scheduler͕ࢦఆ͞Ε͍ͯΔ৔߹͸Ͳ͜Ͱ subscribeΛݺ΅͏͕ɺࢦఆ͞Εͨ Scheduler(εϨου)ͰonΛݺͿɻ

  55. Question4 Ͳ͜ͷεϨουͰಈ࡞͢ΔͰ͠ΐ͏ʁ

  56. None
  57. ֤.Next, .Completed͸ͲͷεϨουͰon͞ΕΔͰ͠ΐ͏ʁ

  58. Answer ਖ਼ղ͸…

  59. A. ح਺͸όοΫάϥ΢ϯυεϨου ۮ਺͸ϝΠϯεϨου .Completed͸ෆఆ

  60. ղઆ • .Nextʹ͍ͭͯ͸౴͑͸؆୯Ͱ͕͢ɺ୭͕Ͳ͏ಈ ͍͍ͯΔͷ͔ΠϝʔδͰ͖ΔͱΑΓྑ͍ͱࢥ͍ ·͢ɻ • ͭ·ΓMergeSinkIter͕subscribeΛ͢Δ → Scheduler͕ࢦఆ͞Ε͍ͯΔͷͰͦͷεϨουͰ on͕ݺ͹Εͯobserverʹ౉͢ͱ͍͏͜ͱͰ͢ɻ

  61. ղઆ • .Completed͸ෆఆͰ͢ɻෳ਺ͷqueueʹ dispatch͍ͯ͠ΔͷͰɺ࠷ޙʹྲྀΕ͖ͯ ͨ.Next͕ͲͷεϨου͔ͩͬͨͰ.Completed ͕ྲྀΕͯ͘ΔεϨου͕มΘΓ·͢ɻ • ·͊ී௨͸͜Μͳ࢖͍ํ͠ͳ͍ͱ͸ࢥ͍·͢ ͕…

  62. Question5 Ͳ͜ͷεϨουͰಈ࡞͢ΔͰ͠ΐ͏ʁ

  63. None
  64. Replay͞ΕΔ.Next͸ 1. .NextΛ࠷ॳʹܭࢉͨ͠εϨου 2. subscribeͨ͠εϨου ͲͪΒͰྲྀΕͯ͘ΔͰ͠ΐ͏ʁ

  65. Answer ਖ਼ղ͸…

  66. A. subscribeͨ͠εϨου

  67. ղઆ • replay͸subscribe͞ΕͨεϨουͰߦΘΕ· ͢ɻ • ·͊ͦΕ͚ͩͰ͢ɻɻɻ

  68. (࠶ܝ) ϧʔϧͦͷ2 createܥͷOperatorʹScheduler͕ࢦ ఆ͞Ε͍ͯΔ৔߹ɺsubscribe͢Δͱͦ ͷScheduler্ͰonΛݺͿɻ ͭ·ΓɺͲͷεϨουͰsubscribe͞Ε ͔ͨ͸ؔ܎ͳ͍ɻ (௕͍)

  69. ObservableͱεϨουʹؔ͢Δϧʔϧ 1. ௨ৗͷColdͳOperator͸subscribe͞ΕͨεϨουͰ subscribeɺon͞ΕͨεϨουͰonΛݺͿɻಛʹ SchedulerͷࢦఆͳͲ͕ͳ͍৔߹ɺcreateܥOperator ͸subscribeͨ͠εϨουͰonΛݺͿ 2. createܥͷOperatorʹScheduler͕ࢦఆ͞Ε͍ͯΔ৔ ߹ɺsubscribe͢ΔͱͦͷScheduler্ͰonΛݺͿɻͭ ·ΓɺͲͷεϨουͰsubscribe͞Ε͔ͨ͸ؔ܎ͳ͍ɻ

  70. ޙฤ observeOn, subscribeOn

  71. observeOn, subscribeOn • ͔͜͜Β͸Scheduler(εϨου)Λมߋ͢Δͨ ΊͷOperator observeOn, subscribeOnʹͭ ͍ͯݟ͍͖ͯ·͕͢ɺ͜͜·Ͱͷ࿩͕ͳΜͱ ͳ͘ཧղͰ͖͍ͯΕ͹ಛʹ೉͘͠ͳ͍ͱࢥ͍ ·͢ɻ

  72. observeOn on͢ΔScheduler(εϨου)Λ มߋ͢Δ

  73. subscribeOn subscribe͢ΔScheduler(εϨου)Λ มߋ͢Δ

  74. ͦͷ··ʂʂ

  75. ղઆ Sequence Map Observer subscribe on ϝΠϯεϨου όοΫάϥ΢ϯυεϨου observeOn .observeOn(backgroundScheduler)

    subscribe subscribe on on
  76. ղઆ Sequence Map Observer subscribe on ϝΠϯεϨου όοΫάϥ΢ϯυεϨου observeOn .observeOn(backgroundScheduler)

    subscribe subscribe on on
  77. ղઆ Sequence Map Observer subscribe ϝΠϯεϨου όοΫάϥ΢ϯυεϨου subscribeOn .subscribeOn(backgroundScheduler) subscribe

    on on on subscribe
  78. ղઆ Sequence Map Observer subscribe ϝΠϯεϨου όοΫάϥ΢ϯυεϨου subscribeOn .subscribeOn(backgroundScheduler) subscribe

    on on on subscribe
  79. observeOn • observeOnʹ͍ͭͯ͸ɺͦΕΑΓલͷจ຺ʹ ؔ܎ͳ͘࢖͑ΔͷͰɺಛʹ೉͍͜͠ͱΛߟ͑ ͣʹʮΠϕϯτ͕ྲྀΕͯ͘ΔεϨουΛม͑ ͍ͨʂʯͱ͍͏ͱ͖ʹ࢖͑Δͱࢥ͍·͢ɻ

  80. subscribeOn • subscribeOnʹ͍ͭͯ͸͔ͳΓ಺෦ͷಈ͖ʹ ґଘ͠·͕͢ɺཁ͸લฤͰΈͨ௨ΓͰɺͬ͟ ͘Γݴͬͯ͠·͑͹ҎԼ͸ಉ͡Ͱ͢ɻ

  81. subscribeOn • લͷจ຺ʹ͔ͳΓґଘ͢ΔͷͰɺͦ΋ͦ΋ subscribeOn͕ޮ͔ͳ͍͜ͱ͢Β΋͋Γ·͢ɻ • ࢖͍υίϩͱͯ͠͸ɺcreateܥͷOperatorͷ ௚ޙʹॻ͍͓͍ͯͯͦͷScheduler(εϨου) Ͱ͔͠ಈ࡞͠ͳ͍ObservableΛ࡞Δ͜ͱ͕Ͱ ͖·͢ɻ

  82. Question6 Ͳ͜ͷεϨουͰಈ࡞͢ΔͰ͠ΐ͏ʁ

  83. None
  84. ͦΕͧΕͷMap͸ͲͷεϨουͰಈ࡞͢ΔͰ͠ΐ͏ʁ

  85. Answer ਖ਼ղ͸…

  86. A. όοΫάϥ΢ϯυεϨου

  87. ղઆ subscribe ϝΠϯεϨου όοΫάϥ΢ϯυεϨου subscribe on on on subscribe Sequence

    Map Observer subscribeOn Map subscribe on
  88. Question7 Ͳ͜ͷεϨουͰಈ࡞͢ΔͰ͠ΐ͏ʁ

  89. None
  90. None
  91. ͦΕͧΕͷMap͸ͲͷεϨουͰಈ࡞͢ΔͰ͠ΐ͏ʁ

  92. Answer ਖ਼ղ͸…

  93. A. ϝΠϯεϨου

  94. ղઆ subscribe ϝΠϯεϨου όοΫάϥ΢ϯυεϨου subscribe subscribe Sequence Map Observer subscribeOn

    Map subscribe
  95. ղઆ subscribe ϝΠϯεϨου όοΫάϥ΢ϯυεϨου subscribe subscribe Sequence Map Observer subscribeOn

    Map subscribe Sequence͸MainSchedulerͰಈ࡞͢Δɻ
  96. ղઆ subscribe ϝΠϯεϨου όοΫάϥ΢ϯυεϨου subscribe subscribe Sequence Map Observer subscribeOn

    Map subscribe on on on on
  97. Question8 Ͳ͜ͷεϨουͰಈ࡞͢ΔͰ͠ΐ͏ʁ

  98. None
  99. ͦΕͧΕͷMap͸ͲͷεϨουͰಈ࡞͢ΔͰ͠ΐ͏ʁ

  100. Answer ਖ਼ղ͸…

  101. A. όοΫάϥ΢ϯυεϨου

  102. ղઆ • (ུ)

  103. (࠶ܝ) observeOn on͢ΔScheduler(εϨου)Λ มߋ͢Δ

  104. (࠶ܝ) subscribeOn subscribe͢ΔScheduler(εϨου)Λ มߋ͢Δ

  105. ͓·͚ Driver

  106. Driver • Driver͸RxCocoa͕ఏڙ͍ͯ͠ΔɺUIͱͭͳ ͛Δͱ͖ʹྑ͍ײ͡ʹ΍ͬͯ͘ΕΔศརͳ Observableతͳ͛͢ʔ΍ͭɻ(ޠኮྗ) • ࡉ͔͍ੑ࣭͸͍Ζ͍Ζ͋ΔΜͰ͕͢ɺεϨο υपΓͷੑ࣭ʹ͍ͭͯݟͯΈ·͢ɻ

  107. Driverͷ࡞Γํ • createܥͷOperatorΛར༻͢Δ • Observable͔Βม׵͢Δ

  108. createܥͷOperatorΛར༻͢Δ • Driverʹ΋ಉ͡Α͏ʹcreate, just, ofͳͲͷ Operator͕ଘࡏ͢Δɻ • ίʔυΛಡΉͱ࢓૊Έ͸؆୯ͰɺϝΠϯεϨο υͰsubscribe͕͞ΕΔΑ͏ʹsubscribeOnΛ ݺΜͰ͍Δ͜ͱ͕Θ͔Δɻ

  109. createܥͷOperatorΛར༻͢Δ

  110. Observable͔Βม׵͢Δ • asDriverͰObservableΛDriverʹͰ͖Δ • ͬͪ͜΋؆୯ͰɺobserveOnͰϝΠϯεϨο υͰಈ࡞͢ΔΑ͏ʹ͍ͯ͠Δɻ

  111. Observable͔Βม׵͢Δ

  112. drive, driveNext • Driver ʹ͸ subscribe ͕ͳ͘ɺ୯७ʹ୅ΘΓ ʹdrive΍ driveNext ͳͲΛར༻͢Δɻ

    • 1͚ͭͩ஫ҙ఺͕͋ͬͯɺ͜ΕΒ͸ϝΠϯε ϨουͰ͔͠ݺ΂ͳ͍ɻ
  113. drive, driveNext

  114. DriverͷOperatorʹ͍ͭͯ • mapͳͲɺ௨ৗͷOperator͸͍͍ͩͨҰ௨Γ ༻ҙ͞Ε͍ͯΔɻ • ͜ΕΒΛݺΜͰ΋ಛʹεϨουʹӨڹ͸ͳ͍ɻ • driveOnSchedulerͱ͍͏Operator͕͋Δ͕ɺ ͜Ε͸ςετ༻ͳͷͰ࢖Θͳ͍Α͏ʹɻ

  115. Question9 Ͳ͜ͷεϨουͰಈ࡞͢ΔͰ͠ΐ͏ʁ

  116. None
  117. Map͸ͲͷεϨουͰಈ࡞͢ΔͰ͠ΐ͏ʁ

  118. Answer ਖ਼ղ͸…

  119. A. Τϥʔ

  120. ղઆ • drive͸ϝΠϯεϨου͔Β͔͠ݺ΂·ͤΜɻ (ͻ͔͚ͬʣ

  121. Question10 Ͳ͜ͷεϨουͰಈ࡞͢ΔͰ͠ΐ͏ʁ

  122. None
  123. Map͸ͲͷεϨουͰಈ࡞͢ΔͰ͠ΐ͏ʁ

  124. Answer ਖ਼ղ͸…

  125. A. ϝΠϯεϨου

  126. ղઆ ϝΠϯεϨου όοΫάϥ΢ϯυεϨου subscribe Sequence Map Observer drive on on

  127. Question11 Ͳ͜ͷεϨουͰಈ࡞͢ΔͰ͠ΐ͏ʁ

  128. None
  129. Map͸ͲͷεϨουͰಈ࡞͢ΔͰ͠ΐ͏ʁ Observerʹ͸ͲͷεϨουͰΠϕϯτ͕ಧ͘Ͱ͠ΐ͏ʁ

  130. Answer ਖ਼ղ͸…

  131. A. map͸όοΫάϥ΢ϯυ observer͸ϝΠϯ

  132. ղઆ ϝΠϯεϨου όοΫάϥ΢ϯυεϨου subscribe Sequence Map Observer drive on on

  133. ͓·͚ ͦͷ2 NSURLSession+Rx

  134. NSURLSession+Rx • rx_response΍rx_JSON͸όοΫάϥ΢ϯυε ϨουͰonΛݺΜͰ͘ΔͷͰɺωοτϫʔΫ → UI Λ΍Δͱ͖͸ؾΛ͚ͭΑ͏ɻ

  135. ·ͱΊ

  136. ObservableͱεϨουʹؔ͢Δϧʔϧ 1. ௨ৗͷColdͳOperator͸subscribe͞ΕͨεϨουͰ subscribeɺon͞ΕͨεϨουͰonΛݺͿɻಛʹ SchedulerͷࢦఆͳͲ͕ͳ͍৔߹ɺcreateܥOperator ͸subscribeͨ͠εϨουͰonΛݺͿ 2. createܥͷOperatorʹScheduler͕ࢦఆ͞Ε͍ͯΔ৔ ߹ɺsubscribe͢ΔͱͦͷScheduler্ͰonΛݺͿɻͭ ·ΓɺͲͷεϨουͰsubscribe͞Ε͔ͨ͸ؔ܎ͳ͍ɻ

  137. observeOn, subscribeOn 1. subscribeOn͸subscribe͢ΔScheduler(ε Ϩου)Λมߋ͢Δɻ 2. observeOn͸on͢ΔScheduler(εϨου)Λ มߋ͢Δɻ

  138. Ҏ্ʂʂ