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.7k
switchのexhaustiveness/redundancy チェック 理論と実装 わいわいswiftc #8 @ukitaka
ukitaka
0
180
SwiftのDemanglerを書く @ わいわいswiftc番外編
ukitaka
0
420
Swiftの型システムに入門する - iOSDC Japan 2018
ukitaka
10
6.6k
Responder Chainを使って コードをスッキリさせたい - 第1回 HAKATA.swift
ukitaka
6
1.3k
理論から入門するswift/lib/Sema - わいわいswiftc #1
ukitaka
5
1.6k
Realmの処理を再利用可能かつ合成可能にする
ukitaka
0
890
マルチスレッドRxSwift @ 社内RxSwift勉強会
ukitaka
5
1.2k
今日こそ理解するHot / Cold @社内RxSwift勉強会
ukitaka
14
2.6k
Other Decks in Programming
See All in Programming
Effective Signals in Angular 19+: Rules and Helpers @ngbe2024
manfredsteyer
PRO
0
130
数十万行のプロジェクトを Scala 2から3に完全移行した
xuwei_k
0
260
Cloudflare MCP ServerでClaude Desktop からWeb APIを構築
kutakutat
1
530
プロダクトの品質に コミットする / Commit to Product Quality
pekepek
2
770
良いユニットテストを書こう
mototakatsu
4
1.7k
命名をリントする
chiroruxx
1
380
[JAWS-UG横浜 #76] イケてるアップデートを宇宙いち早く紹介するよ!
maroon1st
0
450
Go の GC の不得意な部分を克服したい
taiyow
2
760
これでLambdaが不要に?!Step FunctionsのJSONata対応について
iwatatomoya
2
3.6k
たのしいparse.y
ydah
3
120
tidymodelsによるtidyな生存時間解析 / Japan.R2024
dropout009
1
730
Mermaid x AST x 生成AI = コードとドキュメントの完全同期への道
shibuyamizuho
0
160
Featured
See All Featured
KATA
mclloyd
29
14k
JavaScript: Past, Present, and Future - NDC Porto 2020
reverentgeek
47
5.1k
Bash Introduction
62gerente
608
210k
Thoughts on Productivity
jonyablonski
67
4.4k
It's Worth the Effort
3n
183
28k
Designing on Purpose - Digital PM Summit 2013
jponch
116
7k
The World Runs on Bad Software
bkeepers
PRO
65
11k
Producing Creativity
orderedlist
PRO
341
39k
The Cult of Friendly URLs
andyhume
78
6.1k
Learning to Love Humans: Emotional Interface Design
aarron
273
40k
Sharpening the Axe: The Primacy of Toolmaking
bcantrill
38
1.9k
The Psychology of Web Performance [Beyond Tellerrand 2023]
tammyeverts
45
2.2k
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͕༻ҙ͞Ε͍ͯ·͢ ͕ɺͦΕΒ΄΅ಉ͡࡞Γʹͳ͓ͬͯΓɺಡ Έํ͔͑͞Ε؆୯ʹڍಈΛ֬ೝ͢Δ͜ͱ ͕Ͱ͖·͢ɻ • ڍಈʹ͕ࣗͳ͚ΕιʔεΛಡΜͰ֬ೝ͠ Α͏ʂ
Ҏ্ʂ