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
AWS Lambdaから始まった Serverlessの「熱」とキャリアパス / It started with AWS Lambda Serverless “fever” and career path
seike460
PRO
1
250
Enabling DevOps and Team Topologies Through Architecture: Architecting for Fast Flow
cer
PRO
0
310
3rd party scriptでもReactを使いたい! Preact + Reactのハイブリッド開発
righttouch
PRO
1
600
Contemporary Test Cases
maaretp
0
130
AWS IaCの注目アップデート 2024年10月版
konokenj
3
3.3k
『ドメイン駆動設計をはじめよう』のモデリングアプローチ
masuda220
PRO
8
540
RubyLSPのマルチバイト文字対応
notfounds
0
120
WebフロントエンドにおけるGraphQL(あるいはバックエンドのAPI)との向き合い方 / #241106_plk_frontend
izumin5210
4
1.4k
ペアーズにおけるAmazon Bedrockを⽤いた障害対応⽀援 ⽣成AIツールの導⼊事例 @ 20241115配信AWSウェビナー登壇
fukubaka0825
6
1.9k
型付き API リクエストを実現するいくつかの手法とその選択 / Typed API Request
euxn23
8
2.2k
PHP でアセンブリ言語のように書く技術
memory1994
PRO
1
170
よくできたテンプレート言語として TypeScript + JSX を利用する試み / Using TypeScript + JSX outside of Web Frontend #TSKaigiKansai
izumin5210
6
1.7k
Featured
See All Featured
Rebuilding a faster, lazier Slack
samanthasiow
79
8.7k
Speed Design
sergeychernyshev
24
610
Code Review Best Practice
trishagee
64
17k
Teambox: Starting and Learning
jrom
133
8.8k
StorybookのUI Testing Handbookを読んだ
zakiyama
27
5.3k
How GitHub (no longer) Works
holman
310
140k
Designing Experiences People Love
moore
138
23k
RailsConf 2023
tenderlove
29
900
Let's Do A Bunch of Simple Stuff to Make Websites Faster
chriscoyier
506
140k
The Straight Up "How To Draw Better" Workshop
denniskardys
232
140k
The Cult of Friendly URLs
andyhume
78
6k
How to Ace a Technical Interview
jacobian
276
23k
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͕༻ҙ͞Ε͍ͯ·͢ ͕ɺͦΕΒ΄΅ಉ͡࡞Γʹͳ͓ͬͯΓɺಡ Έํ͔͑͞Ε؆୯ʹڍಈΛ֬ೝ͢Δ͜ͱ ͕Ͱ͖·͢ɻ • ڍಈʹ͕ࣗͳ͚ΕιʔεΛಡΜͰ֬ೝ͠ Α͏ʂ
Ҏ্ʂ