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
6.1k
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
1k
Other Decks in Programming
See All in Programming
OSSで起業してもうすぐ10年 / Open Source Conference 2024 Shimane
furukawayasuto
0
110
Hotwire or React? ~アフタートーク・本編に含めなかった話~ / Hotwire or React? after talk
harunatsujita
1
120
watsonx.ai Dojo #4 生成AIを使ったアプリ開発、応用編
oniak3ibm
PRO
1
140
聞き手から登壇者へ: RubyKaigi2024 LTでの初挑戦が 教えてくれた、可能性の星
mikik0
1
130
ローコードSaaSのUXを向上させるためのTypeScript
taro28
1
630
Nurturing OpenJDK distribution: Eclipse Temurin Success History and plan
ivargrimstad
0
960
Generative AI Use Cases JP (略称:GenU)奮闘記
hideg
1
300
Outline View in SwiftUI
1024jp
1
330
タクシーアプリ『GO』のリアルタイムデータ分析基盤における機械学習サービスの活用
mot_techtalk
4
1.5k
A Journey of Contribution and Collaboration in Open Source
ivargrimstad
0
970
C++でシェーダを書く
fadis
6
4.1k
「今のプロジェクトいろいろ大変なんですよ、app/services とかもあって……」/After Kaigi on Rails 2024 LT Night
junk0612
5
2.2k
Featured
See All Featured
Fireside Chat
paigeccino
34
3k
Mobile First: as difficult as doing things right
swwweet
222
8.9k
YesSQL, Process and Tooling at Scale
rocio
169
14k
CoffeeScript is Beautiful & I Never Want to Write Plain JavaScript Again
sstephenson
159
15k
The Success of Rails: Ensuring Growth for the Next 100 Years
eileencodes
44
6.8k
Ruby is Unlike a Banana
tanoku
97
11k
The Illustrated Children's Guide to Kubernetes
chrisshort
48
48k
Intergalactic Javascript Robots from Outer Space
tanoku
269
27k
Save Time (by Creating Custom Rails Generators)
garrettdimon
PRO
27
840
Dealing with People You Can't Stand - Big Design 2015
cassininazir
364
24k
Build The Right Thing And Hit Your Dates
maggiecrowley
33
2.4k
KATA
mclloyd
29
14k
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(εϨου)Λ มߋ͢Δɻ
Ҏ্ʂʂ