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
990
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
5.7k
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
デプロイを任されたので、教わった通りにデプロイしたら障害になった件 ~俺のやらかしを越えてゆけ~
techouse
52
32k
『ドメイン駆動設計をはじめよう』のモデリングアプローチ
masuda220
PRO
7
430
Vue.js学習の振り返り
hiro_xre
2
130
開発効率向上のためのリファクタリングの一歩目の選択肢 ~コード分割~ / JJUG CCC 2024 Fall
ryounasso
0
360
飲食業界向けマルチプロダクトを実現させる開発体制とリアルな現状
hiroya0601
1
390
リリース8年目のサービスの1800個のERBファイルをViewComponentに移行した方法とその結果
katty0324
5
3.6k
2万ページのSSG運用における工夫と注意点 / Vue Fes Japan 2024
chinen
3
1.3k
弊社の「意識チョット低いアーキテクチャ」10選
texmeijin
5
23k
ECS Service Connectのこれまでのアップデートと今後のRoadmapを見てみる
tkikuc
2
210
のびしろを広げる巻き込まれ力:偶然を活かすキャリアの作り方/oso2024
takahashiikki
1
410
Go言語でターミナルフレンドリーなAIコマンド、afaを作った/fukuokago20_afa
monochromegane
2
140
Why Spring Matters to Jakarta EE - and Vice Versa
ivargrimstad
0
960
Featured
See All Featured
CSS Pre-Processors: Stylus, Less & Sass
bermonpainter
355
29k
The Psychology of Web Performance [Beyond Tellerrand 2023]
tammyeverts
41
2.1k
XXLCSS - How to scale CSS and keep your sanity
sugarenia
246
1.3M
CoffeeScript is Beautiful & I Never Want to Write Plain JavaScript Again
sstephenson
159
15k
[RailsConf 2023] Rails as a piece of cake
palkan
51
4.9k
10 Git Anti Patterns You Should be Aware of
lemiorhan
654
59k
Distributed Sagas: A Protocol for Coordinating Microservices
caitiem20
328
21k
ReactJS: Keep Simple. Everything can be a component!
pedronauck
664
120k
Thoughts on Productivity
jonyablonski
67
4.3k
Fontdeck: Realign not Redesign
paulrobertlloyd
81
5.2k
VelocityConf: Rendering Performance Case Studies
addyosmani
325
24k
Sharpening the Axe: The Primacy of Toolmaking
bcantrill
37
1.8k
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͕༻ҙ͞Ε͍ͯ·͢ ͕ɺͦΕΒ΄΅ಉ͡࡞Γʹͳ͓ͬͯΓɺಡ Έํ͔͑͞Ε؆୯ʹڍಈΛ֬ೝ͢Δ͜ͱ ͕Ͱ͖·͢ɻ • ڍಈʹ͕ࣗͳ͚ΕιʔεΛಡΜͰ֬ೝ͠ Α͏ʂ
Ҏ্ʂ