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 18, 2016
Programming
3
1k
RxSwift コードリーディングの勘所@社内RxSwift勉強会
ukitaka
July 18, 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
マルチスレッドRxSwift @ 社内RxSwift勉強会
ukitaka
5
1.2k
今日こそ理解するHot / Cold @社内RxSwift勉強会
ukitaka
14
2.6k
Other Decks in Programming
See All in Programming
最新TCAキャッチアップ
0si43
0
190
Outline View in SwiftUI
1024jp
1
330
Streams APIとTCPフロー制御 / Web Streams API and TCP flow control
tasshi
2
350
Creating a Free Video Ad Network on the Edge
mizoguchicoji
0
120
Tauriでネイティブアプリを作りたい
tsucchinoko
0
370
Better Code Design in PHP
afilina
PRO
0
130
Quine, Polyglot, 良いコード
qnighy
4
650
シェーダーで魅せるMapLibreの動的ラスタータイル
satoshi7190
1
480
Amazon Qを使ってIaCを触ろう!
maruto
0
410
subpath importsで始めるモック生活
10tera
0
310
タクシーアプリ『GO』のリアルタイムデータ分析基盤における機械学習サービスの活用
mot_techtalk
4
1.5k
みんなでプロポーザルを書いてみた
yuriko1211
0
280
Featured
See All Featured
A Tale of Four Properties
chriscoyier
156
23k
Building Applications with DynamoDB
mza
90
6.1k
How To Stay Up To Date on Web Technology
chriscoyier
788
250k
個人開発の失敗を避けるイケてる考え方 / tips for indie hackers
panda_program
93
16k
Build The Right Thing And Hit Your Dates
maggiecrowley
33
2.4k
Statistics for Hackers
jakevdp
796
220k
Building Adaptive Systems
keathley
38
2.3k
Easily Structure & Communicate Ideas using Wireframe
afnizarnur
191
16k
Responsive Adventures: Dirty Tricks From The Dark Corners of Front-End
smashingmag
250
21k
YesSQL, Process and Tooling at Scale
rocio
169
14k
KATA
mclloyd
29
14k
Design and Strategy: How to Deal with People Who Don’t "Get" Design
morganepeng
126
18k
Transcript
RxSwift ίʔυϦʔσΟϯάͷצॴ @ࣾRxSwiftษڧձ
ͳΜͰίʔυಡΉඞཁ͕͋Δͷʁ https://speakerdeck.com/bricklife/rxswiftfalseobserveontosubscribeonwoli-jie-suru
ͳΜͰίʔυಡΉඞཁ͕͋Δͷʁ • ެࣜυΩϡϝϯτ RxMarblesͳͲυΩϡϝϯτ ॆ࣮͍ͯ͠ΔΑ͏ʹݟ͑Δ͕ɺHotมϚϧν εϨουͳͲ͕͖ؔͯͨ࣌͠ͷڍಈυΩϡϝ ϯτΛ͏͚ͩͰཧղ͢Δͷ͍͠ɻ • ·ͨɺRxSwiftಠࣗͷ֓೦OperatorͳͲɺ ReactiveXͷυΩϡϝϯτʹॻ͔Ε͍ͯͳ͍͜ͱ
গ͋͠Δɻ(DriverͳͲ…)
͜ͷεϥΠυͰ • ओʹOperatorͷ࣮Ͱ͘ΘΕ͍ͯΔ֓೦ ख๏Λઆ໌͠·͢ɻ • ͦͷ͋ͱͰ Hot / Cold ͳͲΛྫʹ͋͛ͯɺͲ
͏࣮ͬͯݱ͞Ε͍ͯΔͷ͔Λઆ໌͠·͢ɻ
͜ͷεϥΠυͰ • RxSwift v2.5.0 (20167݄࣌Ͱͷmaster) Λݟͯ·͢ɻ • ͨͿΜόʔδϣϯ1ܥࠓޙͷόʔδϣϯͰ ௨༻͢Δ͔ෆ໌Ͱ͕͢ɺͨͿΜେ͖͘ζϨ Δ͜ͱແ͍ͱࢥ͍·͢ɻ
צॴ ͦͷ1 Operatorʹ͓͚Δ ʮsourceʯ
None
Sequence Map Filter ↑ Α͘Έ͔͚Δਤ
Sequence Map Filter ↑ Α͘Έ͔͚Δਤ ίʔυ্Ͳ͏දݱ͞Ε͍ͯΔʁ
filter
filter ࣮ଶFilterͱ͍͏Ϋϥε
filter sourceͱͯ͠ɺfilterΛݺͼग़ͨ͠ ObservableΛ͍࣋ͬͯΔ
map Mapಉ༷
ͭ·Γ source MapͷsourceSequence (SequenceObservable.ofͷ෦දݱ)
ͭ·Γ source FilterͷsourceMap
ͭ·Γ source Sequence Map Filter source
ͭ·Γ source Sequence Map Filter source sourceͷ࿈ͳΓͱݴ͑Δ
ͱ͜ΖͰɺ source Sequence Map Filter source ͜Εͷsourceʁ
createܥͷOperatorʹsourceͳ͍ • create, just, of, error, never ͳͲɺcreateܥͷ Operatorʹsourceͳ͍ɻ •
mapfilterͷΑ͏ͳҰൠతͳOperatorɺ HotมܥɺsubscribeOn/observeOnʹ source͕͋Δɻ
createܥͷOperatorʹsourceͳ͍ • ͔͜͜Βɺී௨ʹʮOperatorʯͱॻ͍͍ͯ ͨΒʮcreateܥҎ֎ͷOperatorʯͷ͜ͱͩͱ ࢥ͍ͬͯͩ͘͞ɻ
sourceͲ͏ΘΕΔͷʁ • sourceཁObservableͳͷͰɺదͳλΠ ϛϯάͰͦͷsourceΛ(෦తʹ) subscribe͠ ͯΠϕϯτΛड͚औΓ࢝Ί·͢ɻ • ͲͷλΠϛϯάͰ(෦తʹ)subscribe͕ݺ ΕΔ͔ɺHot /
ColdͳͲOperatorͷੑ࣭ʹ Αͬͯҧ͖ͬͯ·͢ɻ
sourceʹ͍ͭͯ·ͱΊ • createܥҎ֎ͷOperatorલͷObservableΛ sourceͱͯ࣋ͭ͜͠ͱͰɺਤͷΑ͏ͳ࿈ͳΓ Λ࣮ݱ͍ͯ͠Δɻ source Sequence Map Filter source
צॴ ͦͷ2 Operator࣮Ͱ νΣοΫ͖͢
Operatorͷ࣮ͰͳʹΛΈΔ͔ ҎԼͲͷOperatorʹڞ௨͢Δಈ͖ • దͳλΠϛϯάͰsourceΛsubscribe͢Δ • source͔Βड͚औͬͨΠϕϯτʹదͳॲཧ ΛՃ͑ͯ࣍ʹྲྀ͢
Operatorͷ࣮ͰͳʹΛΈΔ͔ Sequence Map Filter Observer subscribe
Operatorͷ࣮ͰͳʹΛΈΔ͔ subscribe Sequence Map Filter Observer subscribe subscribe ෦తʹsourceΛsubscribe͢Δ
Operatorͷ࣮ͰͳʹΛΈΔ͔ Sequence Map Filter Observer on on on ΠϕϯτΛड͚औͬͯॲཧΛՃ͑ͯ࣍ʹྲྀ͢
Operatorͷ࣮ͰͳʹΛΈΔ͔ subscribe Sequence Map Filter Observer subscribe subscribe on on
on
Operatorͷ࣮ͰͳʹΛΈΔ͔ ΈΔϙΠϯτ3ͭ • ͍ͭsourceͷsubscribeΛߦ͏͔ • subscribe͕ݺΕͨΒͳʹ͕ى͜Δ͔ • on͕ݺΕͨΒͳʹ͕ى͜Δ͔
ྫ: Filter
ྫ: Filter runsubscribe͕ݺΕͨͱ͖ʹݺΕΔϝιου
ྫ: Filter sinkͱ͔͍͏͕ͭsourceͷsubscribeΛߦ͍ͬͯΔ
ྫ: Filter Sequence Map Filter Observer subscribe ͭ·Γɺࣗͷsubscribe͕ݺΕͨΒ
ྫ: Filter Sequence Map Filter Observer subscribe subscribe sourceͷsubscribeΛߦ͏
ྫ: Filter
ྫ: Filter onsource͔ΒΠϕϯτ͕ྲྀΕ͖ͯͨ࣌ʹݺΕΔϝιου
ྫ: Filter Nextͷ߹
ྫ: Filter ఆ༻ͷؔʹΛͯ͠
ྫ: Filter ݅Λຬ͍ͨͯ͠Ε࣍ྲྀ͢
ྫ: Filter Sequence Map Filter Observer on source͔ΒΠϕϯτ͕དྷͨΒ
ྫ: Filter Sequence Map Filter Observer on on ఆͨ͠ޙ࣍͢
ಡΊΔʂ ✌('ω'✌ )ࡾ✌('ω')✌ࡾ( ✌'ω')✌
·ͱΊ ΈΔϙΠϯτ3ͭ • ͍ͭsourceͷsubscribeΛߦ͏͔ • subscribe͕ݺΕͨΒͳʹ͕ى͜Δ͔ • on͕ݺΕͨΒͳʹ͕ى͜Δ͔
Subject… ಉ͘͡3ͭΛΈΔͱڍಈ͕͔ͭΊΔͱࢥ͍·͢ɻ • ͍ͭsourceͷsubscribeΛߦ͏͔ • subscribe͕ݺΕͨΒͳʹ͕ى͜Δ͔ • on͕ݺΕͨΒͳʹ͕ى͜Δ͔
createܥͷOperator… ͱΓ͋͑ͣɺ • subscribe͕ݺΕͨΒͳʹ͕ى͜Δ͔ ͜ΕΛݟ͓͚ͯڍಈ͕Θ͔Δͱࢥ͍·͢ɻ
צॴ ͦͷ3 Sink / SinkIter
(෮श) Operatorͷಈ͖ • దͳλΠϛϯάͰsourceΛsubscribe͢Δ • source͔Βड͚औͬͨΠϕϯτʹదͳॲཧ ΛՃ͑ͯ࣍ʹྲྀ͢
(෮श) Operatorͷಈ͖ • దͳλΠϛϯάͰsourceΛsubscribe͢Δ • source͔Βड͚औͬͨΠϕϯτʹదͳॲཧ ΛՃ͑ͯ࣍ʹྲྀ͢ ୭͕subscribe͢Δͷʁobserver୭ʁ
Filter
Filter
Filter
Sink
Sinkͱʁ • (෦తʹ)sourceͷobserverʹͳΔͭ • FilterʹFilterSinkɺMapʹMapSinkɺͷΑ ͏ʹ΄΅શͯͷOperatorʹରͯ̋̋͠Sink͕ ଘࡏ͢Δ
෦తͳಈ͖ (subscribe࣌)
Sequence Map Filter Observer
Filter Sequence Map Filter Observer subscribe͕ݺΕͨΒrun͕ݺΕΔ subscribe(observer)
Filter Sequence Map Filter Observer subscribe(observer) observer͕ͦͷ··͞ΕΔ
Filter Sequence Map Filter Observer observerΛݩʹFilterSink͕࡞ΒΕΔ FilterSink(observer)
Filter Sequence Map Filter Observer FilterSink͕sourceΛsubscribe͢Δ source.subscribe(FilterSink(observer))
Map Sequence Map Filter Observer source.subscribe(FilterSink(observer))
Map Sequence Map Filter Observer source.subscribe(FilterSink(observer)) observerͱͯ͠ɺFilterSink(observer)͕ͬͯ͘Δɻ
Map Sequence Map Filter Observer MapSink(FilterSink(observer)) observerΛݩʹFilterSink͕࡞ΒΕΔ
Map Sequence Map Filter Observer source.subscribe(MapSink(FilterSink(observer))) MapSink͕sourceΛsubscribe͢Δ
෦తͳಈ͖ (on࣌)
MapSink Sequence Map Filter Observer Πϕϯτ͕ૹग़͞ΕΔ MapSink(FilterSink(observer)).on(.Next(2))
MapSink Sequence Map Filter Observer Πϕϯτ͕selectorʹΑͬͯม͞ΕΔ .Next(2) → .Next(4)
MapSink Sequence Map Filter Observer Πϕϯτ͕ૹग़͞ΕΔ FilterSink(observer).on(.Next(4))
FilterSink Sequence Map Filter Observer Πϕϯτ͕ૹग़͞ΕΔ FilterSink(observer).on(.Next(4))
FilterSink Sequence Map Filter Observer ݅νΣοΫ͞ΕΔ value % 4 ==
0
FilterSink Sequence Map Filter Observer Πϕϯτ͕ૹग़͞ΕΔ observer.on(.Next(4))
ಡΊΔʂ ✌('ω'✌ )ࡾ✌('ω')✌ࡾ( ✌'ω')✌
·ͱΊ subscribe(MapSink(FilterSink(observer))) Sequence Map Filter Observer subscribe(FilterSink(observer)) subscribe(observer) observerΛSinkʹแΈͳ͕Β sourceΛsubscribe
·ͱΊ Sequence Map Filter Observer observer.on(.Next) FilterSink(observer).on(.Next) MapSink(FilterSink(observer)).on(.Next) SinkͰॲཧΛՃ͑ͳ͕Β observerʹΠϕϯτΛྲྀ͢
SinkIter
SinkIterͱʁ • mergeflatMapͳͲɺ(গͳ͘ͱ෦తʹ) Observable<Observable<E>> Λѻ͏ͱ͖ʹ ར༻͞ΕΔɻ
SinkIterͱʁ Observable<Observable<E>>
SinkIterͱʁ Observable<Observable<E>> ֎ଆͷObservableΛ subscribe͢Δͷ͕Sink
SinkIterͱʁ Observable<Observable<E>> ଆͷObservableΛ subscribe͢Δͷ͕SinkIter
ಡΜͰΈΑ͏ ColdͳObservable
ColdͳObservableͷੑ࣭ • ͍͔ͭ͋͘Γ·͕͢ɺࠓճʮsubscribe͠ͳ ͍ͱಈ࡞͠ͳ͍ʯͱ͍͏ੑ࣭͕ίʔυϨϕϧ ͰͲ͏࣮ݱ͞Ε͍ͯΔ͔Λ֬ೝͯ͠Έ·͢ɻ • ݴ͍͔͑Εʮsubscribe͞Εͨͱ͖ʹಈ࡞Λ ࢝ΊΔʯͰ͢ɻ
Just
Just subscribe͞ΕΔͱ….
Just ͕ૹग़͞ΕΔʂʂʂ
Cold
Cold subscribe Just Map Filter Observer subscribe subscribe on on
on
ಡΜͰΈΑ͏ BehaviorSubject
BehaviorSubjectͷੑ࣭ • ͍͔ͭ͋͘Γ·͕͢ɺࠓճʮذͰ͖Δʯ ʮsubscribe࣌ʹલͷ͕औΕΔʯΛ֬ೝ͠ ͯΈ·͢ɻ
BehaviorSubject લͷΛอ࣋͢Δม
BehaviorSubject ෳͷobserver
BehaviorSubject subscribe͞ΕͨΒݺΕΔ
BehaviorSubject subscribe͞ΕͨΒobserverͱͯ͠ొ
BehaviorSubject લͷΛૹग़
BehaviorSubject onͰΠϕϯτΛड͚औͬͨͱ͖ʹݺΕΔ
BehaviorSubject લͷΛߋ৽
BehaviorSubject ֤Observerʹૹग़
ಡΊΔʂ ✌('ω'✌ )ࡾ✌('ω')✌ࡾ( ✌'ω')✌
·ͱΊ • Rxʹଟ͘ͷOperator͕༻ҙ͞Ε͍ͯ·͢ ͕ɺͦΕΒ΄΅ಉ͡࡞Γʹͳ͓ͬͯΓɺಡ Έํ͔͑͞Ε؆୯ʹڍಈΛ֬ೝ͢Δ͜ͱ ͕Ͱ͖·͢ɻ • ڍಈʹ͕ࣗͳ͚ΕιʔεΛಡΜͰ֬ೝ͠ Α͏ʂ
Ҏ্ʂ