Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Features
Speaker Deck
PRO
Sign in
Sign up for free
Search
Search
マルチスレッドRxSwift @ 社内RxSwift勉強会
Search
ukitaka
July 19, 2016
Programming
5
1.2k
マルチスレッドRxSwift @ 社内RxSwift勉強会
マルチスレッドRxSwift @ 社内RxSwift勉強会
ukitaka
July 19, 2016
Tweet
Share
More Decks by ukitaka
See All by ukitaka
CSの生産性改善を支える分析環境 Mercari CS/CRE Tech Talk #1
ukitaka
1
2.6k
switchのexhaustiveness/redundancy チェック 理論と実装 わいわいswiftc #8 @ukitaka
ukitaka
0
180
SwiftのDemanglerを書く @ わいわいswiftc番外編
ukitaka
0
410
Swiftの型システムに入門する - iOSDC Japan 2018
ukitaka
10
5.7k
Responder Chainを使って コードをスッキリさせたい - 第1回 HAKATA.swift
ukitaka
6
1.3k
理論から入門するswift/lib/Sema - わいわいswiftc #1
ukitaka
5
1.6k
Realmの処理を再利用可能かつ合成可能にする
ukitaka
0
880
今日こそ理解するHot / Cold @社内RxSwift勉強会
ukitaka
14
2.6k
RxSwift コードリーディングの勘所@社内RxSwift勉強会
ukitaka
3
990
Other Decks in Programming
See All in Programming
OpenTelemetryでRailsのパフォーマンス分析を始めてみよう(KoR2024)
ymtdzzz
4
1.5k
offers_20241022_imakiire.pdf
imakurusu
2
360
Kotlin2でdataクラスの copyメソッドを禁止する/Data class copy function to have the same visibility as constructor
eichisanden
1
130
gopls を改造したら開発生産性が高まった
satorunooshie
8
240
Sidekiqで実現する 長時間非同期処理の中断と再開 / Pausing and Resuming Long-Running Asynchronous Jobs with Sidekiq
hypermkt
6
2.7k
Webの技術スタックで マルチプラットフォームアプリ開発を可能にするElixirDesktopの紹介
thehaigo
2
910
役立つログに取り組もう
irof
26
8.6k
Content Security Policy入門 セキュリティ設定と 違反レポートのはじめ方 / Introduction to Content Security Policy Getting Started with Security Configuration and Violation Reporting
uskey512
1
420
Streams APIとTCPフロー制御 / Web Streams API and TCP flow control
tasshi
1
290
Why Spring Matters to Jakarta EE - and Vice Versa
ivargrimstad
0
960
Piniaの現状と今後
waka292
5
1.4k
カスタムしながら理解するGraphQL Connection
yanagii
1
1.2k
Featured
See All Featured
ReactJS: Keep Simple. Everything can be a component!
pedronauck
664
120k
Visualization
eitanlees
144
15k
Visualizing Your Data: Incorporating Mongo into Loggly Infrastructure
mongodb
42
9.2k
The Pragmatic Product Professional
lauravandoore
31
6.3k
A designer walks into a library…
pauljervisheath
202
24k
Fight the Zombie Pattern Library - RWD Summit 2016
marcelosomers
231
17k
Statistics for Hackers
jakevdp
796
220k
Practical Orchestrator
shlominoach
186
10k
Done Done
chrislema
181
16k
Bootstrapping a Software Product
garrettdimon
PRO
305
110k
10 Git Anti Patterns You Should be Aware of
lemiorhan
654
59k
Designing Experiences People Love
moore
138
23k
Transcript
ϚϧνεϨουRxSwift @ࣾRxSwiftษڧձ
߹ΘͤͯಡΈ͍ͨ https://https://speakerdeck.com/ukitaka/rxswift-kodorideingufalsekan-suo-at-she-nei-rxswiftmian-qiang-hui/bricklife/rxswiftfalseobserveontosubscribeonwoli-jie-suru
ཁίʔυϦʔσΟϯά • ޙͰৄ͘͠આ໌͠·͕͢ɺʮͲͷεϨουͰ subscribe͕ߦΘΕ͔ͨʯ͕ͱͯॏཁʹͳ͖ͬͯ ·͢ɻ • ͜͜Ͱ͍͏subscribe෦తͳsubscribeؚΈ ·͢ɻͭ·ΓʮOperator͕͍ͭͲͷεϨουͰ sourceΛsubscribe͢Δ͔ʯΛѲ͠ͳ͍ͱɺڍಈ Λཧղ͢Δͷ͕͍͜͠ͱ͋Δ͔͠Ε·ͤΜɻ
͜ͷεϥΠυͰ • ·ͣɺRxSwiftͱGCDͳͲͷΈ߹ΘͤʹΑΔ ϚϧνεϨουڥͰͷڍಈͷ֬ೝͱಈ࡞ϧʔ ϧͷઆ໌Λߦ͍·͢ɻ • ͦͷޙobserveOnɺsubscribeOnͷڍಈͷઆ ໌Λߦ͍·͢ɻ
͜ͷεϥΠυͰ • ϚϧνεϨουͱ͍ͬͯɺଟ͘ͷiOS։ൃऀ͕ڵຯ͕͋Δͷ ʮϝΠϯεϨουʯ͔ʮͦΕҎ֎͔ʯͩͱࢥ͍·͢ɻ • ͜ͷεϥΠυͰɺʮϝΠϯεϨουͰͳ͍εϨουʯΛ·ͱ ΊͯʮόοΫάϥϯυεϨουʯͱݺͿ͜ͱʹͯ͠ɺϝΠϯ εϨου͔όοΫάϥϯυεϨου͔ͷΈʹ͠·͢ɻ • ͭ·ΓόοΫάϥϯυεϨουෳ͋Γɺ࣮ࡍʹͲͷεϨο
υͰॲཧ͞ΕΔ͔ɺ(RxSwift͕෦తʹར༻͍ͯ͠Δ) GCD NSOperationQueueʹґଘ͢Δ͜ͱʹҙ͍ͯͩ͘͠͞ɻ
͜ͷεϥΠυͰ • RxSwift v2.5.0 (20167݄࣌Ͱͷmaster) Λݟͯ·͢ɻ • ͨͿΜόʔδϣϯ1ܥࠓޙͷόʔδϣϯͰ ௨༻͢Δ͔ෆ໌Ͱ͕͢ɺͨͿΜେ͖͘ζϨ Δ͜ͱແ͍ͱࢥ͍·͢ɻ
લฤ Observableͱ εϨου
ಈ࡞ϧʔϧ
ϧʔϧͦͷ1 ௨ৗͷColdͳOperator subscribe͞ΕͨεϨουͰsubscribe on͞ΕͨεϨουͰonΛݺͿɻ
ϧʔϧͦͷ1(ଓ͖) ಛʹSchedulerͷࢦఆͳͲ͕ͳ͍߹ɺ createܥOperatorsubscribeͨ͠ε ϨουͰonΛݺͿɻ
ݴ༿͚ͩͩͱΑ͘Θ͔ΒΜ
None
Sequence Observer
Sequence Observer subscribe ϝΠϯεϨου όοΫάϥϯυεϨου
Sequence Observer subscribe ϝΠϯεϨου όοΫάϥϯυεϨου on
Question1 Ͳ͜ͷεϨουͰಈ࡞͢ΔͰ͠ΐ͏ʁ
None
MapͲͷεϨουͰಈ࡞͢ΔͰ͠ΐ͏ʁ
Answer ਖ਼ղ…
A. όοΫάϥϯυεϨου
ղઆ Sequence Map Observer subscribe ϝΠϯεϨου όοΫάϥϯυεϨου
ղઆ subscribe Sequence Map Observer subscribe ϝΠϯεϨου όοΫάϥϯυεϨου ݺΕͨεϨουͰ෦తʹsourceͷ subscribeΛߦ͏ɻ
ղઆ subscribe Sequence Map Observer subscribe ϝΠϯεϨου όοΫάϥϯυεϨου subscribe͕ݺΕͨεϨουonΛݺͿ
ղઆ subscribe Sequence Map Observer subscribe on ϝΠϯεϨου όοΫάϥϯυεϨου on
Question2 Ͳ͜ͷεϨουͰಈ࡞͢ΔͰ͠ΐ͏ʁ
None
MapͲͷεϨουͰಈ࡞͢ΔͰ͠ΐ͏ʁ
Answer ਖ਼ղ…
A. όοΫάϥϯυεϨου
ղઆ Sequence Map Observer Publish
ղઆ Sequence Map Observer Publish subscribe
ղઆ Sequence Map Observer subscribe Publish subscribe
ղઆ subscribe Sequence Map Observer subscribe Publish subscribe
ղઆ subscribe Sequence Map Observer subscribe Publish subscribe subscribe͕ݺΕͨεϨουonΛݺͿ
ղઆ subscribe Sequence Map Observer subscribe on on Publish subscribe
on
(ิ) ղઆ subscribe Sequence Map Observer subscribe on on Publish
subscribe on ಛʹઆ໌ͯ͠ͳ͔͕ͬͨɺSubject on͞ΕͨεϨουͰonΛݺͿ
Question3 Ͳ͜ͷεϨουͰಈ࡞͢ΔͰ͠ΐ͏ʁ
None
MapͲͷεϨουͰಈ࡞͢ΔͰ͠ΐ͏ʁ
Answer ਖ਼ղ…
A. όοΫάϥϯυεϨου
ղઆ • refCount() ʮHot͕ͩɺ࠷ॳʹsubscribe͞ ΕΔ·Ͱಈ࡞͠ͳ͍ʯͱ͍͏ڍಈɻͭ·Γ࠷ ॳʹsubscribe͕ݺΕͨεϨου͕ॏཁɻ • ࠓճ࠷ॳʹsubscribeΛݺΜͰ͍Δͷ͕όο ΫάϥϯυεϨουͳͷͰόοΫάϥϯ υͰಈ࡞͢Δɻ
(࠶ܝ) ϧʔϧͦͷ1 ௨ৗͷColdͳOperator subscribe͞ΕͨεϨουͰsubscribe on͞ΕͨεϨουͰonΛݺͿɻ
(࠶ܝ) ϧʔϧͦͷ1(ଓ͖) ಛʹSchedulerͷࢦఆͳͲ͕ͳ͍߹ɺ createܥOperatorsubscribeͨ͠ε ϨουͰonΛݺͿɻ
࣍ͷϧʔϧ…
ϧʔϧͦͷ2 createܥͷOperatorʹScheduler͕ࢦ ఆ͞Ε͍ͯΔ߹ɺsubscribe͢Δͱͦ ͷScheduler্ͰonΛݺͿɻ ͭ·ΓɺͲͷεϨουͰsubscribe͞Ε ͔ͨؔͳ͍ɻ (͍)
Scheduler • createܥͷOperatorʹɺSchedulerΛࢦఆͰ ͖Δͷ͕͋Δɻ
Scheduler • Scheduler͕ࢦఆ͞Ε͍ͯΔ߹ʹͲ͏͍͏ڍ ಈʹͳΔ͔ɺͱ͍͏ͷ͕ࠓճͷϧʔϧ
None
Scheduler͕ࢦఆ͞Ε͍ͯΔ
Sequence Observer subscribe ϝΠϯεϨου όοΫάϥϯυεϨου
Sequence Observer subscribe ϝΠϯεϨου όοΫάϥϯυεϨου
Sequence Observer subscribe ϝΠϯεϨου όοΫάϥϯυεϨου on
ͭ·Γ • Scheduler͕ࢦఆ͞Ε͍ͯΔ߹Ͳ͜Ͱ subscribeΛݺ΅͏͕ɺࢦఆ͞Εͨ Scheduler(εϨου)ͰonΛݺͿɻ
Question4 Ͳ͜ͷεϨουͰಈ࡞͢ΔͰ͠ΐ͏ʁ
None
֤.Next, .CompletedͲͷεϨουͰon͞ΕΔͰ͠ΐ͏ʁ
Answer ਖ਼ղ…
A. حόοΫάϥϯυεϨου ۮϝΠϯεϨου .Completedෆఆ
ղઆ • .Nextʹ͍ͭͯ͑؆୯Ͱ͕͢ɺ୭͕Ͳ͏ಈ ͍͍ͯΔͷ͔ΠϝʔδͰ͖ΔͱΑΓྑ͍ͱࢥ͍ ·͢ɻ • ͭ·ΓMergeSinkIter͕subscribeΛ͢Δ → Scheduler͕ࢦఆ͞Ε͍ͯΔͷͰͦͷεϨουͰ on͕ݺΕͯobserverʹ͢ͱ͍͏͜ͱͰ͢ɻ
ղઆ • .CompletedෆఆͰ͢ɻෳͷqueueʹ dispatch͍ͯ͠ΔͷͰɺ࠷ޙʹྲྀΕ͖ͯ ͨ.Next͕ͲͷεϨου͔ͩͬͨͰ.Completed ͕ྲྀΕͯ͘ΔεϨου͕มΘΓ·͢ɻ • ·͊ී௨͜Μͳ͍ํ͠ͳ͍ͱࢥ͍·͢ ͕…
Question5 Ͳ͜ͷεϨουͰಈ࡞͢ΔͰ͠ΐ͏ʁ
None
Replay͞ΕΔ.Next 1. .NextΛ࠷ॳʹܭࢉͨ͠εϨου 2. subscribeͨ͠εϨου ͲͪΒͰྲྀΕͯ͘ΔͰ͠ΐ͏ʁ
Answer ਖ਼ղ…
A. subscribeͨ͠εϨου
ղઆ • replaysubscribe͞ΕͨεϨουͰߦΘΕ· ͢ɻ • ·͊ͦΕ͚ͩͰ͢ɻɻɻ
(࠶ܝ) ϧʔϧͦͷ2 createܥͷOperatorʹScheduler͕ࢦ ఆ͞Ε͍ͯΔ߹ɺsubscribe͢Δͱͦ ͷScheduler্ͰonΛݺͿɻ ͭ·ΓɺͲͷεϨουͰsubscribe͞Ε ͔ͨؔͳ͍ɻ (͍)
ObservableͱεϨουʹؔ͢Δϧʔϧ 1. ௨ৗͷColdͳOperatorsubscribe͞ΕͨεϨουͰ subscribeɺon͞ΕͨεϨουͰonΛݺͿɻಛʹ SchedulerͷࢦఆͳͲ͕ͳ͍߹ɺcreateܥOperator subscribeͨ͠εϨουͰonΛݺͿ 2. createܥͷOperatorʹScheduler͕ࢦఆ͞Ε͍ͯΔ ߹ɺsubscribe͢ΔͱͦͷScheduler্ͰonΛݺͿɻͭ ·ΓɺͲͷεϨουͰsubscribe͞Ε͔ͨؔͳ͍ɻ
ޙฤ observeOn, subscribeOn
observeOn, subscribeOn • ͔͜͜ΒScheduler(εϨου)Λมߋ͢Δͨ ΊͷOperator observeOn, subscribeOnʹͭ ͍ͯݟ͍͖ͯ·͕͢ɺ͜͜·Ͱͷ͕ͳΜͱ ͳ͘ཧղͰ͖͍ͯΕಛʹ͘͠ͳ͍ͱࢥ͍ ·͢ɻ
observeOn on͢ΔScheduler(εϨου)Λ มߋ͢Δ
subscribeOn subscribe͢ΔScheduler(εϨου)Λ มߋ͢Δ
ͦͷ··ʂʂ
ղઆ Sequence Map Observer subscribe on ϝΠϯεϨου όοΫάϥϯυεϨου observeOn .observeOn(backgroundScheduler)
subscribe subscribe on on
ղઆ Sequence Map Observer subscribe on ϝΠϯεϨου όοΫάϥϯυεϨου observeOn .observeOn(backgroundScheduler)
subscribe subscribe on on
ղઆ Sequence Map Observer subscribe ϝΠϯεϨου όοΫάϥϯυεϨου subscribeOn .subscribeOn(backgroundScheduler) subscribe
on on on subscribe
ղઆ Sequence Map Observer subscribe ϝΠϯεϨου όοΫάϥϯυεϨου subscribeOn .subscribeOn(backgroundScheduler) subscribe
on on on subscribe
observeOn • observeOnʹ͍ͭͯɺͦΕΑΓલͷจ຺ʹ ؔͳ͑͘ΔͷͰɺಛʹ͍͜͠ͱΛߟ͑ ͣʹʮΠϕϯτ͕ྲྀΕͯ͘ΔεϨουΛม͑ ͍ͨʂʯͱ͍͏ͱ͖ʹ͑Δͱࢥ͍·͢ɻ
subscribeOn • subscribeOnʹ͍͔ͭͯͳΓ෦ͷಈ͖ʹ ґଘ͠·͕͢ɺཁલฤͰΈͨ௨ΓͰɺͬ͟ ͘Γݴͬͯ͠·͑ҎԼಉ͡Ͱ͢ɻ
subscribeOn • લͷจ຺ʹ͔ͳΓґଘ͢ΔͷͰɺͦͦ subscribeOn͕ޮ͔ͳ͍͜ͱ͢Β͋Γ·͢ɻ • ͍υίϩͱͯ͠ɺcreateܥͷOperatorͷ ޙʹॻ͍͓͍ͯͯͦͷScheduler(εϨου) Ͱ͔͠ಈ࡞͠ͳ͍ObservableΛ࡞Δ͜ͱ͕Ͱ ͖·͢ɻ
Question6 Ͳ͜ͷεϨουͰಈ࡞͢ΔͰ͠ΐ͏ʁ
None
ͦΕͧΕͷMapͲͷεϨουͰಈ࡞͢ΔͰ͠ΐ͏ʁ
Answer ਖ਼ղ…
A. όοΫάϥϯυεϨου
ղઆ subscribe ϝΠϯεϨου όοΫάϥϯυεϨου subscribe on on on subscribe Sequence
Map Observer subscribeOn Map subscribe on
Question7 Ͳ͜ͷεϨουͰಈ࡞͢ΔͰ͠ΐ͏ʁ
None
None
ͦΕͧΕͷMapͲͷεϨουͰಈ࡞͢ΔͰ͠ΐ͏ʁ
Answer ਖ਼ղ…
A. ϝΠϯεϨου
ղઆ subscribe ϝΠϯεϨου όοΫάϥϯυεϨου subscribe subscribe Sequence Map Observer subscribeOn
Map subscribe
ղઆ subscribe ϝΠϯεϨου όοΫάϥϯυεϨου subscribe subscribe Sequence Map Observer subscribeOn
Map subscribe SequenceMainSchedulerͰಈ࡞͢Δɻ
ղઆ subscribe ϝΠϯεϨου όοΫάϥϯυεϨου subscribe subscribe Sequence Map Observer subscribeOn
Map subscribe on on on on
Question8 Ͳ͜ͷεϨουͰಈ࡞͢ΔͰ͠ΐ͏ʁ
None
ͦΕͧΕͷMapͲͷεϨουͰಈ࡞͢ΔͰ͠ΐ͏ʁ
Answer ਖ਼ղ…
A. όοΫάϥϯυεϨου
ղઆ • (ུ)
(࠶ܝ) observeOn on͢ΔScheduler(εϨου)Λ มߋ͢Δ
(࠶ܝ) subscribeOn subscribe͢ΔScheduler(εϨου)Λ มߋ͢Δ
͓·͚ Driver
Driver • DriverRxCocoa͕ఏڙ͍ͯ͠ΔɺUIͱͭͳ ͛Δͱ͖ʹྑ͍ײ͡ʹͬͯ͘ΕΔศརͳ Observableతͳ͛͢ʔͭɻ(ޠኮྗ) • ࡉ͔͍ੑ࣭͍Ζ͍Ζ͋ΔΜͰ͕͢ɺεϨο υपΓͷੑ࣭ʹ͍ͭͯݟͯΈ·͢ɻ
Driverͷ࡞Γํ • createܥͷOperatorΛར༻͢Δ • Observable͔Βม͢Δ
createܥͷOperatorΛར༻͢Δ • Driverʹಉ͡Α͏ʹcreate, just, ofͳͲͷ Operator͕ଘࡏ͢Δɻ • ίʔυΛಡΉͱΈ؆୯ͰɺϝΠϯεϨο υͰsubscribe͕͞ΕΔΑ͏ʹsubscribeOnΛ ݺΜͰ͍Δ͜ͱ͕Θ͔Δɻ
createܥͷOperatorΛར༻͢Δ
Observable͔Βม͢Δ • asDriverͰObservableΛDriverʹͰ͖Δ • ͬͪ͜؆୯ͰɺobserveOnͰϝΠϯεϨο υͰಈ࡞͢ΔΑ͏ʹ͍ͯ͠Δɻ
Observable͔Βม͢Δ
drive, driveNext • Driver ʹ subscribe ͕ͳ͘ɺ୯७ʹΘΓ ʹdrive driveNext ͳͲΛར༻͢Δɻ
• 1͚ͭͩҙ͕͋ͬͯɺ͜ΕΒϝΠϯε ϨουͰ͔͠ݺͳ͍ɻ
drive, driveNext
DriverͷOperatorʹ͍ͭͯ • mapͳͲɺ௨ৗͷOperator͍͍ͩͨҰ௨Γ ༻ҙ͞Ε͍ͯΔɻ • ͜ΕΒΛݺΜͰಛʹεϨουʹӨڹͳ͍ɻ • driveOnSchedulerͱ͍͏Operator͕͋Δ͕ɺ ͜Εςετ༻ͳͷͰΘͳ͍Α͏ʹɻ
Question9 Ͳ͜ͷεϨουͰಈ࡞͢ΔͰ͠ΐ͏ʁ
None
MapͲͷεϨουͰಈ࡞͢ΔͰ͠ΐ͏ʁ
Answer ਖ਼ղ…
A. Τϥʔ
ղઆ • driveϝΠϯεϨου͔Β͔͠ݺ·ͤΜɻ (ͻ͔͚ͬʣ
Question10 Ͳ͜ͷεϨουͰಈ࡞͢ΔͰ͠ΐ͏ʁ
None
MapͲͷεϨουͰಈ࡞͢ΔͰ͠ΐ͏ʁ
Answer ਖ਼ղ…
A. ϝΠϯεϨου
ղઆ ϝΠϯεϨου όοΫάϥϯυεϨου subscribe Sequence Map Observer drive on on
Question11 Ͳ͜ͷεϨουͰಈ࡞͢ΔͰ͠ΐ͏ʁ
None
MapͲͷεϨουͰಈ࡞͢ΔͰ͠ΐ͏ʁ ObserverʹͲͷεϨουͰΠϕϯτ͕ಧ͘Ͱ͠ΐ͏ʁ
Answer ਖ਼ղ…
A. mapόοΫάϥϯυ observerϝΠϯ
ղઆ ϝΠϯεϨου όοΫάϥϯυεϨου subscribe Sequence Map Observer drive on on
͓·͚ ͦͷ2 NSURLSession+Rx
NSURLSession+Rx • rx_responserx_JSONόοΫάϥϯυε ϨουͰonΛݺΜͰ͘ΔͷͰɺωοτϫʔΫ → UI ΛΔͱ͖ؾΛ͚ͭΑ͏ɻ
·ͱΊ
ObservableͱεϨουʹؔ͢Δϧʔϧ 1. ௨ৗͷColdͳOperatorsubscribe͞ΕͨεϨουͰ subscribeɺon͞ΕͨεϨουͰonΛݺͿɻಛʹ SchedulerͷࢦఆͳͲ͕ͳ͍߹ɺcreateܥOperator subscribeͨ͠εϨουͰonΛݺͿ 2. createܥͷOperatorʹScheduler͕ࢦఆ͞Ε͍ͯΔ ߹ɺsubscribe͢ΔͱͦͷScheduler্ͰonΛݺͿɻͭ ·ΓɺͲͷεϨουͰsubscribe͞Ε͔ͨؔͳ͍ɻ
observeOn, subscribeOn 1. subscribeOnsubscribe͢ΔScheduler(ε Ϩου)Λมߋ͢Δɻ 2. observeOnon͢ΔScheduler(εϨου)Λ มߋ͢Δɻ
Ҏ্ʂʂ