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
川で例えずに理解するObservable
Search
Yoshikuni Kato
February 14, 2017
Programming
3
2.5k
川で例えずに理解するObservable
Presented at 集まれSwift好き!Swift愛好会 vol16
https://love-swift.connpass.com/event/48820/
Yoshikuni Kato
February 14, 2017
Tweet
Share
More Decks by Yoshikuni Kato
See All by Yoshikuni Kato
The Elm Architecture & Swift
yoching
0
890
iOS developers community in Tokyo
yoching
0
640
Swiftエンジニアが海外のポジションに応募する
yoching
10
2.9k
App Architecture By Manual DI
yoching
0
650
Passing function to function arguments
yoching
0
620
「新規アプリの設計」を設計する
yoching
1
1.8k
App Architecture By Manual DI
yoching
2
530
関数を引数として渡す書き方のポイント
yoching
0
760
App Architecture Sample
yoching
0
92
Other Decks in Programming
See All in Programming
PHPで作るWebSocketサーバー ~リアクティブなアプリケーションを知るために~ / WebSocket Server in PHP - To know reactive applications
seike460
PRO
2
450
PHPで学ぶプログラミングの教訓 / Lessons in Programming Learned through PHP
nrslib
3
280
Amazon S3 NYJavaSIG 2024-12-12
sullis
0
100
わたしの星のままで一番星になる ~ 出産を機にSIerからEC事業会社に転職した話 ~
kimura_m_29
0
180
Webエンジニア主体のモバイルチームの 生産性を高く保つためにやったこと
igreenwood
0
340
競技プログラミングへのお誘い@阪大BOOSTセミナー
kotamanegi
0
360
Effective Signals in Angular 19+: Rules and Helpers @ngbe2024
manfredsteyer
PRO
0
140
【re:Growth 2024】 Aurora DSQL をちゃんと話します!
maroon1st
0
780
Haze - Real time background blurring
chrisbanes
1
510
採用事例の少ないSvelteを選んだ理由と それを正解にするためにやっていること
oekazuma
2
1k
見えないメモリを観測する: PHP 8.4 `pg_result_memory_size()` とSQL結果のメモリ管理
kentaroutakeda
0
380
バグを見つけた?それAppleに直してもらおう!
uetyo
0
180
Featured
See All Featured
Exploring the Power of Turbo Streams & Action Cable | RailsConf2023
kevinliebholz
28
4.4k
Code Reviewing Like a Champion
maltzj
520
39k
10 Git Anti Patterns You Should be Aware of
lemiorhan
PRO
656
59k
Save Time (by Creating Custom Rails Generators)
garrettdimon
PRO
28
900
Testing 201, or: Great Expectations
jmmastey
40
7.1k
Why You Should Never Use an ORM
jnunemaker
PRO
54
9.1k
Keith and Marios Guide to Fast Websites
keithpitt
410
22k
GitHub's CSS Performance
jonrohan
1030
460k
Unsuck your backbone
ammeep
669
57k
RailsConf & Balkan Ruby 2019: The Past, Present, and Future of Rails at GitHub
eileencodes
132
33k
Understanding Cognitive Biases in Performance Measurement
bluesmoon
26
1.5k
Rails Girls Zürich Keynote
gr2m
94
13k
Transcript
Ͱྫ͑ͣʹཧղ͢ΔObservable @yoshikuni_kato Swi$Ѫձ vol16 2017/02/14! 1
Who am I? • Ճ౻༝܇ʢYoshikuni Katoʣ @yoshikuni_kato • iOSΤϯδχΞ •
Yahoo! Japan -> Φϋί • ʮϥδ͐ʯ͘Μ → • Interests: ઃܭ / FRP / Coordinator PaCern / UI࣮ • try! SwiFʢ&ϋοΧιϯʣߦ͖·͢ʂ 2
Agenda 1. ͡Ίʹɹ~ObservableͷΑ͋͘Δྫ͑~ 2. Ͱྫ͑ͣʹཧղ͢ΔObservable 3. Hot / ColdʢReac7veSwi;ͷεεϝʣ 3
͡Ίʹ 4
Func%onal Reac%ve Programming • ϓϩάϥϛϯάͷύϥμΠϜ • swi% libraries • Reac.veSwi%
(Reac.veCocoa) ɹɹ˞ Reac.veCocoa3͔Βswi%Խ͞Ε͍ͯ·͢ʢݱࡏɺ5ʣ ɹɹ˞ 5ͰϦϙδτϦ͕ཧ͞ΕɺຊମʢReac.veSwi%ʣͱUIKit֦ு ʢReac.veCocoaʣͱͳΓ·ͨ͠ • RxSwi% • RxͱݺͣʹFRPͱݺͼ͍ͨ 5
Observableͱ • Observable ɹ= Stream of values over 2me ɹ࣌ؒ࣠ʹԊͬͯൃੜ͢ΔͷετϦʔϜ
1 ! Observableͬͯɺʮ؍Ͱ͖Δʯͬͯ ͍͏ҙຯͩΑͳ͊ɾɾɾ ɹʮ࣌ؒʯʮετϦʔϜʯͱͲ͏͍͏ ؔͳΜͩΖ͏ʁ 1 The introduc.on to Reac.ve Programming you've been missing 6
Α͋͘Δྫ͑ • Observable = ! ͬͯʢιϑτΣΞతʹʣԿʁ ɹ ਫͲ͔͜ΒྲྀΕͯ͘Δͷʁ ɹ
ྲྀΕͨਫͲ͜ʹߦͬͯ͠·͏ͷʁ ɹ HotͱCold͕͋ΔΈ͍͚ͨͩͲɾɾɾɾ 7
ྫ͑Δ͜ͱͷޭࡑ ! - ͱ͖ͬͭͦ͢͏ʹࢥ͑Δ ! - ؒҧͬͨʢ࣮ͱҟͳΔʣཧղΛͯ͠͠·͏Մೳੑ͕͋Δ - தͷιʔεΛͲ͏ಡΜͰ͍͍͔͔Βͳ͍ ιϑτΣΞతͳߏΛཧղ͢Δ͜ͱॏཁ
8
Ͱྫ͑ͣʹཧղ͢ΔObservable 9
ऍ • FRPڞ௨ͷߟ͑ํʢͷͣʣ • Reac(veSwi-ϕʔε • RxSwi-ͷํιʔεΛ͖ͪΜͱಡΊͳ͔ͬͨͷͰɺؒҧͬͯ ͍ͨΒ͢Έ·ͤΜ! • ྫͱ্ͯ͛͠Δίʔυ͔ͳΓ؆ུԽͨ͠ͷ
10
Observerύλʔϯ͕جૅ 11
Observer = closure ʢΫϩʔδϟʔͷബ͍ϥούʔͱ࣮ͯ͠ʣ • ͲΜͳΫϩʔδϟʔ͔ʁ Event -> Void {
event: Event in // logics when changed } 12
Event // ReactiveSwift-1.0 ͷ߹ enum Event<Value, Error> { // RxSwiftͰErrorͷࢦఆͳ͠
case value(Value) // RxSwiftͷ߹next case failed(Error) // RxSwiftͷ߹ error(Swift.Error) case completed case interrupted // ReactiveSwiftͰͳ͠ } 13
Observable = Observerͷొઌ • Observable means "؍Ͱ͖Δͷ" • Observerͷcollec0onΛ͍࣋ͬͯΔ //
ΠϝʔδͰ͢ʂʂ class Observable { var observers: [Observer] } ※ ࣮ࡍʹɺλμͷྻͰͳ͘ɺͬͱෳࡶͳػߏͰ͢ 14
observe = ObserverΛొ͢Δ Observer(closure)ΛɺObservableͷObserver collec/onʹՃ͢Δ ※ Rxܥͩͱ"subscribe"ͱ͍͏໊લ 15
Observable = Stream of values over 1me ͱ ઌʹΫϩʔδϟʔ͕ొ͞Εͯ͋Δ ʹ
ʢObservableଆ͔ΒݟΕʣ ɹɹҙͷλΠϛϯάͰɺొ͞ΕͨΫϩʔδϟʔΛ࣮ߦͰ͖Δ → ࣌ؒʹԊͬͯൃੜ͢ΔΛදݱͰ͖Δ → มͦͷͷ͕ɺετϦʔϜʢΛநԽͨ͠ͷʣͱଊ͑ΒΕΔ ※ खଓ͖ܕతͳʮม͕ϝϞϦྖҬ(orΦϒδΣΫτ)Λ͍ࣔͯ͠Δʯ ͱ͍͏͜ͱΑΓந͕ߴ͍ 16
1 1 The introduc.on to Reac.ve Programming you've been missing
17
ʢ༨ஊʣ໋໊͕͔Γʹ͍͘ʁ ӳޠతɿObserver observe observable. (S-V-O) ίʔυɿobservable.observe(observer) ӳޠతͳҙຯͱ࣮ࡍͷڍಈ͕ର ʢຊདྷɺobservable.observed(by: observer)ɹͳͲʣ Reac%veSwi*Ͱଟগٞʹͳ͍ͬͯΔ
2 2 Rename Signal.observe(_:) 18
operator = observable -> observableͷม • map / filter /
flatMap / combineLatestͳͲͳͲɾɾɾ • RxMarble / RACMarbleΛࢀর 19
ʮҙͷλΠϛϯάͰ࣮ߦ͢ΔʯͷΈ Reac%veSwi*ͷ߹ • Signal(HotObservable)innerObserverͱηοτͰ࡞ΒΕɺinnerObserverSignalͷࢀরΛ࣋ͭ • innerObserverΛ࣮ߦ͢ΔʢobserverʹΠϕϯτΛ͢ʣ -> SignalͷobserversΛશͯʹΠϕϯτΛ͢ // Πϝʔδ
let innerObserver = { event in for observer in signal.observers { observer.send(event) } } • operator(map/filter/...)ɺ͜ͷinnerObserverΛɺલͷSignalͷobserverʹొ͢ΔΑ͏ͳܗ 20
Hot / Cold ʢReac%veSwi*ͷεεϝʣ 21
Hot / Cold • Hot / Coldʢ֓೦ʣ͔ΒೖΔͱ͔Γʹ͍͘ • Reac.veSwi3ͷߟ͑ํ͕ࢀߟʹͳΔ 22
Reac%veSwi*(Cocoa)ʹ͓͚Δ Hot/Cold • Reac&veCocoa2·ͰɿHot/Cold྆ํಉ͡ܕʢࠓͷRxͱಉ༷ʣ • Reac&veCocoa3Ҏ߱ɿHot/ColdΛܕͰ • Hot : Signal
• Cold: SignalProducer ͭ·ΓʮHotObservable ͱ ͦͷੜثʯͱཧղͰ͖Δ 23
Signal / SignalProducerʹ͍ͭͯ Qiitaʹ࠷ۙهࣄΛॻ͖·ͨ͠ Reac%veSwi* جຊཁૉͷ͍ํ Signal / SignalProducerฤ 24
Reac%veSwi* vs RxSwi* 25
Reac%veSwi* vs RxSwi* ! • Hot / Coldͷѻ͍͕͔Γ͍͢ • Τϥʔͷܕ͕ࢦఆͰ͖Δ
• ֮͑Δܕ͕গͳ͍ʢSignal/SignalProducer/Property/Ac7on/Disposable͘Β ͍ʣ • தͷ࣮ͷɺϑΝΠϧ͕গͳ͘ɺಡΈͦ͢͏ͳҹʢʹͳͬͨʣ • Carthageͷ࣮ʹ༻͞Ε͍ͯΔ 26
Reac%veSwi*ͷϑΝΠϧߏ 27
Reac%veSwi* vs RxSwi* ! • ຊޠͷυΩϡϝϯτ͕গͳ͍ • ݕࡧͯ͠ɺݹ͍هࣄ(Obj-C)͕ώοτ͕ͪ͠ • όʔδϣϯʹΑΔഁյతมߋ͕ଟ͍
• ίϛϡχςΟ͕ऑΊʢRealmͱͷ࿈ܞͳͲʣ • ΦϑΟγϟϧͷαϯϓϧͳͲ͕ͳ͍ 28
·ͱΊ 29
·ͱΊ • FRPΦϒβʔόʔύλʔϯ͕ࠜఈʹ͋Δ • ObservableͰΠϕϯτετϦʔϜͦͷͷΛදͤΔ • ιϑτΣΞతͳߏΛཧղ͢Δ͜ͱॏཁ • Reac.veSwi2ʢReac.veCocoaʣͬͯΈ͍ͯͩ͘͞! 30
iOSΤϯδχΞืूத • ࣄۀɿUX/UIσβΠϯ & ։ൃ ɹ ʢࠓͷͱ͜Ζʣ৽نडୗ։ൃϝΠϯ • ࣾһɿ20໊ڧʢiOSΤϯδχΞ 3໊ʣ
• ͪΖΜSwi/ • ։ൃͷࣗ༝ߴ͍ • try ! Swi/ (16/17) / iOSDC εϙϯαʔ • UI࣮͖ͳਓɺઃܭ͖ͳਓٻΉ 31
Thank you! 32
ิ 33
ࢲ͕Reac%veSwi*Λ͍࢝ΊͨܦҢ • ࠷ॳʢ2016/3݄͝ΖʣRxSwi-ΛʢྲྀߦΓͰʣษڧ • Reac1veSwi-Meetup@WantedlyͰReac1veCocoaͷΛฉ͖ɺ ઃܭ͕ྑͦ͞͏ͩͱࢥ͍ɺ͍࢝ΊΔ • @inamy͞Μɺ@ikesyo͞ΜɺNick͞Μ • ࠓޙչ͍ͯ͠ͳ͍
34
ࢀߟ • Reac&veCocoa/Reac&veSwi. • Reac&veCocoa/Reac&veCocoa • RACCommunity • Reac&veX/RxSwi. •
Reac&ve Swi. Meetup @WantedlyΛ։࠵͠·ͨ͠ʂ • @ikesyo͞Μͷࢿྉ • Reac&veSwi. جຊཁૉͷ͍ํ Signal / SignalProducerฤ 35