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
快速入門可觀測性
blueswen
0
360
Mermaid x AST x 生成AI = コードとドキュメントの完全同期への道
shibuyamizuho
0
160
17年周年のWebアプリケーションにTanStack Queryを導入する / Implementing TanStack Query in a 17th Anniversary Web Application
saitolume
0
250
Recoilを剥がしている話
kirik
5
6.7k
fs2-io を試してたらバグを見つけて直した話
chencmd
0
230
LLM Supervised Fine-tuningの理論と実践
datanalyticslabo
6
1.2k
【re:Growth 2024】 Aurora DSQL をちゃんと話します!
maroon1st
0
780
生成AIでGitHubソースコード取得して仕様書を作成
shukob
0
430
開発者とQAの越境で自動テストが増える開発プロセスを実現する
92thunder
1
190
テストケースの名前はどうつけるべきか?
orgachem
PRO
0
130
「とりあえず動く」コードはよい、「読みやすい」コードはもっとよい / Code that 'just works' is good, but code that is 'readable' is even better.
mkmk884
3
390
PHPUnitしか使ってこなかった 一般PHPerがPestに乗り換えた実録
mashirou1234
0
190
Featured
See All Featured
GraphQLとの向き合い方2022年版
quramy
44
13k
Speed Design
sergeychernyshev
25
670
The Cost Of JavaScript in 2023
addyosmani
45
7k
Into the Great Unknown - MozCon
thekraken
33
1.5k
Practical Tips for Bootstrapping Information Extraction Pipelines
honnibal
PRO
10
810
Reflections from 52 weeks, 52 projects
jeffersonlam
347
20k
Producing Creativity
orderedlist
PRO
341
39k
Docker and Python
trallard
42
3.1k
Automating Front-end Workflow
addyosmani
1366
200k
For a Future-Friendly Web
brad_frost
175
9.4k
Designing Experiences People Love
moore
138
23k
[Rails World 2023 - Day 1 Closing Keynote] - The Magic of Rails
eileencodes
33
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͕༻ҙ͞Ε͍ͯ·͢ ͕ɺͦΕΒ΄΅ಉ͡࡞Γʹͳ͓ͬͯΓɺಡ Έํ͔͑͞Ε؆୯ʹڍಈΛ֬ೝ͢Δ͜ͱ ͕Ͱ͖·͢ɻ • ڍಈʹ͕ࣗͳ͚ΕιʔεΛಡΜͰ֬ೝ͠ Α͏ʂ
Ҏ্ʂ