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
テストケースの名前はどうつけるべきか?
orgachem
PRO
0
130
Асинхронность неизбежна: как мы проектировали сервис уведомлений
lamodatech
0
700
KubeCon + CloudNativeCon NA 2024 Overviewat Kubernetes Meetup Tokyo #68 / amsy810_k8sjp68
masayaaoyama
0
250
Mermaid x AST x 生成AI = コードとドキュメントの完全同期への道
shibuyamizuho
0
160
Stackless и stackful? Корутины и асинхронность в Go
lamodatech
0
690
あれやってみてー駆動から成長を加速させる / areyattemite-driven
nashiusagi
1
200
フロントエンドのディレクトリ構成どうしてる? Feature-Sliced Design 導入体験談
osakatechlab
8
4.1k
DevFest Tokyo 2025 - Flutter のアプリアーキテクチャ現在地点
wasabeef
5
900
From Translations to Multi Dimension Entities
alexanderschranz
2
130
20年もののレガシープロダクトに 0からPHPStanを入れるまで / phpcon2024
hirobe1999
0
140
Security_for_introducing_eBPF
kentatada
0
110
複雑な仕様に立ち向かうアーキテクチャ
myohei
0
170
Featured
See All Featured
Speed Design
sergeychernyshev
25
670
The Straight Up "How To Draw Better" Workshop
denniskardys
232
140k
Building Flexible Design Systems
yeseniaperezcruz
327
38k
For a Future-Friendly Web
brad_frost
175
9.4k
Adopting Sorbet at Scale
ufuk
73
9.1k
Learning to Love Humans: Emotional Interface Design
aarron
273
40k
Music & Morning Musume
bryan
46
6.2k
Stop Working from a Prison Cell
hatefulcrawdad
267
20k
Product Roadmaps are Hard
iamctodd
PRO
49
11k
Optimizing for Happiness
mojombo
376
70k
A Tale of Four Properties
chriscoyier
157
23k
[RailsConf 2023 Opening Keynote] The Magic of Rails
eileencodes
28
9.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