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
880
iOS developers community in Tokyo
yoching
0
640
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
Creating a Free Video Ad Network on the Edge
mizoguchicoji
0
120
ヤプリ新卒SREの オンボーディング
masaki12
0
130
エンジニアとして関わる要件と仕様(公開用)
murabayashi
0
300
Why Jakarta EE Matters to Spring - and Vice Versa
ivargrimstad
0
1.1k
Arm移行タイムアタック
qnighy
0
330
Outline View in SwiftUI
1024jp
1
330
Enabling DevOps and Team Topologies Through Architecture: Architecting for Fast Flow
cer
PRO
0
340
『ドメイン駆動設計をはじめよう』のモデリングアプローチ
masuda220
PRO
8
540
ローコードSaaSのUXを向上させるためのTypeScript
taro28
1
630
よくできたテンプレート言語として TypeScript + JSX を利用する試み / Using TypeScript + JSX outside of Web Frontend #TSKaigiKansai
izumin5210
6
1.7k
3 Effective Rules for Using Signals in Angular
manfredsteyer
PRO
0
100
見せてあげますよ、「本物のLaravel批判」ってやつを。
77web
7
7.8k
Featured
See All Featured
Producing Creativity
orderedlist
PRO
341
39k
Designing for Performance
lara
604
68k
Being A Developer After 40
akosma
87
590k
Fight the Zombie Pattern Library - RWD Summit 2016
marcelosomers
232
17k
Large-scale JavaScript Application Architecture
addyosmani
510
110k
The Success of Rails: Ensuring Growth for the Next 100 Years
eileencodes
44
6.8k
Designing the Hi-DPI Web
ddemaree
280
34k
What's in a price? How to price your products and services
michaelherold
243
12k
Automating Front-end Workflow
addyosmani
1366
200k
A better future with KSS
kneath
238
17k
Fontdeck: Realign not Redesign
paulrobertlloyd
82
5.2k
Side Projects
sachag
452
42k
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