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.4k
川で例えずに理解する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
870
iOS developers community in Tokyo
yoching
0
630
Swiftエンジニアが海外のポジションに応募する
yoching
10
2.9k
App Architecture By Manual DI
yoching
0
640
Passing function to function arguments
yoching
0
610
「新規アプリの設計」を設計する
yoching
1
1.8k
App Architecture By Manual DI
yoching
2
520
関数を引数として渡す書き方のポイント
yoching
0
750
App Architecture Sample
yoching
0
92
Other Decks in Programming
See All in Programming
CSC509 Lecture 08
javiergs
PRO
0
110
讓數據說話:用 Python、Prometheus 和 Grafana 講故事
eddie
0
350
カラム追加で増えるActiveRecordのメモリサイズ イメージできますか?
asayamakk
4
1.5k
【Kaigi on Rails 2024】YOUTRUST スポンサーLT
krpk1900
1
240
Kaigi on Rails 2024 - Rails APIモードのためのシンプルで効果的なCSRF対策 / kaigionrails-2024-csrf
corocn
5
3.4k
Kotlin2でdataクラスの copyメソッドを禁止する/Data class copy function to have the same visibility as constructor
eichisanden
1
130
Universal Linksの実装方法と陥りがちな罠
kaitokudou
1
220
PagerDuty を軸にした On-Call 構築と運用課題の解決 / PagerDuty Japan Community Meetup 4
horimislime
1
110
Pinia Colada が実現するスマートな非同期処理
naokihaba
2
150
2万ページのSSG運用における工夫と注意点 / Vue Fes Japan 2024
chinen
3
1.3k
Dev ContainersとGitHub Codespacesの素敵な関係
ymd65536
1
130
Content Security Policy入門 セキュリティ設定と 違反レポートのはじめ方 / Introduction to Content Security Policy Getting Started with Security Configuration and Violation Reporting
uskey512
1
420
Featured
See All Featured
Easily Structure & Communicate Ideas using Wireframe
afnizarnur
191
16k
Bash Introduction
62gerente
608
210k
Optimising Largest Contentful Paint
csswizardry
33
2.9k
YesSQL, Process and Tooling at Scale
rocio
167
14k
We Have a Design System, Now What?
morganepeng
50
7.2k
A Philosophy of Restraint
colly
203
16k
Building a Modern Day E-commerce SEO Strategy
aleyda
38
6.9k
Why You Should Never Use an ORM
jnunemaker
PRO
53
9k
Java REST API Framework Comparison - PWX 2021
mraible
PRO
28
7.9k
Stop Working from a Prison Cell
hatefulcrawdad
267
20k
Building Applications with DynamoDB
mza
90
6.1k
How to Create Impact in a Changing Tech Landscape [PerfNow 2023]
tammyeverts
46
2.1k
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