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
カッコ悪くリアクティブじゃダメですか?
Search
tokorom
April 13, 2016
Technology
7
12k
カッコ悪くリアクティブじゃダメですか?
レガシーな既存プロジェクトへのRxSwift導入事例
tokorom
April 13, 2016
Tweet
Share
More Decks by tokorom
See All by tokorom
続・SharePlayの歴史と進化 iOS18とApple Vision Proにおける新展開
tokorom
3
560
SharePlayの歴史と進化 そしてvisionOSへ (iOSDC 2023)
tokorom
2
880
5分でSharePlay入門
tokorom
2
1.4k
HomeKit 2020
tokorom
7
2.4k
Advanced Segue 2019年のSegue事情
tokorom
9
5.8k
tvOSアプリUIの勘所
tokorom
1
2k
古き良きsendAction (in potatotips #26)
tokorom
1
3.6k
画面遷移と私(iOS)
tokorom
4
3.7k
長生きするために心臓に悪いリリースはもうやめよう
tokorom
21
33k
Other Decks in Technology
See All in Technology
B2B SaaSから見た最近のC#/.NETの進化
sansantech
PRO
0
910
あなたの知らない Function.prototype.toString() の世界
mizdra
PRO
0
120
rootlessコンテナのすゝめ - 研究室サーバーでもできる安全なコンテナ管理
kitsuya0828
3
390
DynamoDB でスロットリングが発生したとき_大盛りver/when_throttling_occurs_in_dynamodb_long
emiki
1
440
Lexical Analysis
shigashiyama
1
150
【令和最新版】AWS Direct Connectと愉快なGWたちのおさらい
minorun365
PRO
5
760
OCI 運用監視サービス 概要
oracle4engineer
PRO
0
4.8k
Zennのパフォーマンスモニタリングでやっていること
ryosukeigarashi
0
160
Platform Engineering for Software Developers and Architects
syntasso
1
520
OCI Network Firewall 概要
oracle4engineer
PRO
0
4.2k
Lambda10周年!Lambdaは何をもたらしたか
smt7174
2
110
ノーコードデータ分析ツールで体験する時系列データ分析超入門
negi111111
0
420
Featured
See All Featured
Principles of Awesome APIs and How to Build Them.
keavy
126
17k
Fashionably flexible responsive web design (full day workshop)
malarkey
405
65k
Save Time (by Creating Custom Rails Generators)
garrettdimon
PRO
27
840
Done Done
chrislema
181
16k
Distributed Sagas: A Protocol for Coordinating Microservices
caitiem20
329
21k
Visualization
eitanlees
145
15k
Keith and Marios Guide to Fast Websites
keithpitt
409
22k
Understanding Cognitive Biases in Performance Measurement
bluesmoon
26
1.4k
Exploring the Power of Turbo Streams & Action Cable | RailsConf2023
kevinliebholz
27
4.3k
How GitHub (no longer) Works
holman
310
140k
Fight the Zombie Pattern Library - RWD Summit 2016
marcelosomers
232
17k
Ruby is Unlike a Banana
tanoku
97
11k
Transcript
Χοίѱ͘ ϦΞΫςΟϒ ͡ΌμϝͰ͔͢ʁ ॴ ༑ଠ @tokorom iOS App Programer Reactive
Swift Meetup
ॴ༑ଠ @tokorom ɾϑϦʔλʔ ɾΧοίѱ͍ϦΞΫςΟϒ෩ϓϩάϥϚ ɾpotatotipsӡӦ૭ޱʢओ࠵ऀ͞Μ͍ͭืूதʣ
ΧοίΠΠ ϦΞΫςΟϒϓϩάϥϛϯά FRP RxSwift ReSwift Redux Flux
ʢ࣭ʣ •͓ࣄͰɺطଘϓϩδΣΫτͷόʔδϣϯΞο ϓ͕ϝΠϯʁ •ʹҰճ͘Β͍৽نϓϩδΣΫτʹθϩ͔Β ܞΘΕΔͤऀʁ
΅͘ͷϦΞΫςΟϒϓϩάϥϛϯάʁ •ϓϩδΣΫτ։࢝࣌͘͝ී௨ͷMVCߏ •όʔδϣϯΞοϓ࣌ʹϦΞΫςΟϒϓϩάϥϛ ϯά༻ϥΠϒϥϦಋೖ •ϦϦʔεؒࡍʹύϑΥʔϚϯεൃ֮ •RxSwiftʹࠩ͠ସ͑ •ݱঢ়ɺ҆ఆಈ࡞ •ؾʹೖͬͯΔ
ී௨ͷMVCߏ •ModelManager UserManager TopicManager XXXManager XXXViewController XXXViewController
ී௨ͷMVCߏ •ModelManager UserManager TopicManager XXXManager XXXViewController XXXViewController XXXManager໋໊ͬͯͲ͏ͳͷ !?
ී௨ͷMVCߏ •ModelManager UserManager TopicManager XXXManager XXXViewController XXXViewController Topic Topic Topic
ී௨ͷMVCߏ •ModelManager UserManager TopicManager XXXManager XXXViewController XXXViewController Topic Topic Topic
class Topic { let identifier: String var title: String? var
isHearted: Bool } Modelͷߋ৽Λࢹ •Μͩ݁Ռ -> ModelΛ KVO class Topic: NSObject { let identifier: String dynamic var title: String? dynamic var isHearted: Bool } ग़ͨʔʂ Swift࣌ʹ NSObjectʂ
topic.rx_observe(Bool.self, "isHearted") .subscribeNext { [weak self] in self?.heartButton?.selected = $0
?? false } //.addDisposableTo(disposeBag) Modelͷߋ৽Λࢹ •ࢹଆαϯϓϧʢViewʣ ͑ͬ!? ViewModel ?
Θ͍Θ͍ ϦΞΫςΟϒ ϓϩάϥϛϯά ͩͬʂʂʂ ͜Ε ϦΞΫςΟϒ ϓϩάϥϛϯά ͳͷʂʁ
KVO + αʁ •͍ͬͯΔ͜ͱݟฦͨ͠ΒɺͨͩKVOͰViewͱ ModelΛόΠϯσΟϯάͯ͠Δ͚ͩ •debounce ͱ͔ distinctUntilChanged ͱ͔ Rxతศརػೳ͋ΔͷͰRxSwift͏ํָָ͕
•ͰʹཱͭͳΒͦΕͰྑ͍ʁ ͳΜ͔Χοίѱ͍͚ͲͶ
ʹཱͭʢ۩ମతʹʁʣ •όά͕ݮΔ? ɹ=> ݮͬͨʂ •ָ࣮͕ʹͳΔ? => ָʹͳͬͨʂ
࣮ࡍʹྑ͍ޮՌ͕͋ͬͨͱ͜Ζ •ࢄΒΔὑͪΌΜͷղܾ •௨৴ଓ෮׆͞Μͷղܾ
ࢄΒΔὑͪΌΜ •ৄࡉը໘Ͱὑͯ͠Ϧετը໘ʹͬͨΒʁ
ࢄΒΔὑͪΌΜ •ϕλͳ࣮ •viewWillAppear + visibleCellsͰὑ͞Ε͍ͯΔ͔ νΣοΫ •cellForRowAtIndexPathͰὑ͞Ε͍ͯΔ͔νΣοΫ •willDisplayCellͰὑ͞Ε͍ͯΔ͔νΣοΫ
ࢄΒΔὑͪΌΜ •ὑ͢Δ௨৴தʹը໘ભҠͪ͠ΌͬͨΒʁ
ࢄΒΔὑͪΌΜ •ϕλͳ࣮ •௨৴ޭ࣌ʹNSNotificationͰ௨͢Δʁ
ࢄΒΔὑͪΌΜ •RxSwiftͳΒͳʹߟ͑ͳ͍͍ͯ͘ topic.rx_observe(Bool.self, "isHearted") .subscribeNext { [weak self] in self?.heartButton?.selected
= $0 ?? false } //.addDisposableTo(disposeBag) ܁Γฦ͚͢ͲɺͨͩͷKVO͔ͩΒͶ
௨৴ଓ෮׆͞Μ •௨৴ΤϥʔͰίϯςϯπະऔಘͷ͚࣌ͩɺ௨৴ ଓ෮׆ޙʹίϯςϯπ࠶औಘ͍ͨ͠
௨৴ଓ෮׆͞Μ •ϕλͳ࣮ •௨৴ଓ෮׆ΛNSNotificationͳͲͰϋϯυϦϯά •ͦͷͱ͖ίϯςϯπऔಘࣦഊޙͰɺ͔ͭɺطʹ௨৴ தͰͳ͚Ε௨৴Λ࠶ΒͤΔ •։ൃ͍ͯ͠Δͱ͞Βʹ͕݅ՃΘͬͯ͘Δ •௨৴։࢝ͷτϦΨʔͱ͕݅Β͚ͯࡶʹͳΓ͕ ͪ
௨৴ଓ෮׆͞Μ •RxSwiftͳΒҰݩཧͰ͖Δ let a = Reachability.rx_reachable.asObservable() let b = rx_contentState.asObservable()
Observable.combineLatest(a, b) { ($0, $1) } .filter { reachable, contentState in reachable && contentState == .Unload } .subscribeNext { [weak self] _ in self?.reloadDataIfNeeded() }
ϦΞΫςΟϒ෩ϓϩάϥϛϯά ͰޮՌ͋Γͦ͏ :) ͰΧοίѱ͍͚ͲͶ
Χοίѱࣦ͘ഊͨ͠ͱ͜Ζ •উखʹߋ৽ͪΌΜ •εΫϩʔϧ͔͔ͬ͘͘Μ
উखʹߋ৽ͪΌΜ •ͱ͋Δը໘Λ։͍ͨޙʹҙਤͤͣ༰͕มΘΔ
উखʹߋ৽ͪΌΜ •͜ͷྫ୯ʹόΠϯσΟϯά͕ෆదͳ͚ͩ •ϓϩΞΫςΟϒ →ϦΞΫςΟϒͳΒͰͷෆ۩ ߹͕͋Δ͜ͱ͋Δ •ͨͩ͠ϓϩΞΫςΟϒΦϯϦʔͷ࣌ͷෆ۩߹ͷ ΄͏͕ଟ͍
εΫϩʔϧ͔͔ͬ͘͘Μ •͍͟ಈ͔ͯ͠ΈͨΒύϑΥʔϚϯεѱ͍ •UICollectionViewCellʹରͯ͠εΫϩʔϧͷʹ όΠϯσΟϯάͷॲཧ͕ΔͨΊ •͜Ε͕ղܾͰ͖ͳͯ͘RxSwiftΛಋೖͨ͠ΒͳΜ ͱ͔ͳͬͨ
εΫϩʔϧ͔͔ͬ͘͘Μ •ར༻͢ΔϥΠϒϥϦ࣮ͷ͔ͨ͠ʹΑͬͯม Θͬͯ͘ΔͷͰࣄલʹςετ •ྫ͑ࠜݩࣗͷ͖ͳ࣮ʹͯ͠ɺόΠϯ σΟϯάͷͱ͜Ζ͚ͩSwiftBondΛར༻͢Δͱ ͍ͬͨղܾͷํ๏ߟ͑ΒΕΔ
࣮ࡍʹϓϩδΣΫτͰͬͯΈͯͷॴײ •࣮͕γϯϓϧʹͳͬͨ •ಛʹෳͷλΠϛϯά݅ʹΑΓ࣮ߦ͖͢ॲཧ •ෆ۩߹͕50%Ҏ্ݮ͍ͬͯΔͷͰʢମײʣ •ࠓޙɺϦΞΫςΟϒͳ࣮ΛΘͳ͍͜ͱߟ ͑ͮΒ͍
Χοίѱ͘ ϦΞΫςΟϒ ͡ΌμϝͰ͔͢ʁ
มΘ͍ͬͯ͘͜ͱ •ઈࢍਐԽத •ReactKit / SwiftBond / RxSwift •The Evolution of
Flux Framework (20155݄ʣ •SwiftFlux / Swift Flow / ReduxKit / ReSwift •̍ޙʹ·ͩݟ͵ϥΠϒϥϦͬͯΔʁ •RxSwiftྑ͍ײ͡ʹރΕ͖ͯͯΔ͔
ΩʔϫʔυͷτϨϯυ •MVVM •React / Flux / Redux •ϨΨγʔͳModelManager + KVO
ʹࣅͯΔʁ
มΘΒͳ͍͜ͱ •ϦΞΫςΟϒͷ༗༻ੑ •ϓϩΞΫςΟϒͷ༗༻ੑ
ऄ •KVOͱ͔ݹ͍Έʁ͏ͷͬͯͲ͏ͳͷʁ •ΠϯλʔϑΣʔεͳͲ͔֬ʹݹ͍͕ར༻͢Δ ͷѱ͘ͳ͍ͷͰ •ͨͩɺiOS͕ඪ४Ͱඋ͑ΔͷͰ།Ұແೋͳϝ Ϧοτ͕͋ͬͨΓ͢Δ •ϥοϐϯάͳͲͯ͠ར༻͢Δ͜ͱͰΠϯλʔ ϑΣʔεͷ؇Ͱ͖Δ෦͋Δ
KVO •ίϯύΠϧ࣌ʹkeyPathޡΓΛൃݟͰ͖ͳ͍ •ղܾํ๏͋ΔͩΖ͏͚Ͳ… •࣮ߦ࣌ʹException͕ൃੜ͢Δ: addObserver:<RxCocoa.KVOObserver 0x7f8133b500b0> forKeyPath:@"hoge" options: 5 context:0x0]
… •ύϑΥʔϚϯεྑ͘লϝϞϦ
sendAction (Responder Chain) •RxSwiftʹΈࠐ·Ε͍ͯͳ͍͕ϥοϐϯά͠ ͯར༻͢ΕϝϦοτ͕ग़ͯ͘Δ͔͠Εͳ͍ʁ
UIApplication - sendAction NEW! iOS 9 Ͱ ྲྀ ͷ͝ͱ͘ݱΕͨ ࠷ڧ
ͷ function !?
UIApplication - sendAction iPhone OS 2 ͔Β ͘͝ ී௨ ʹଘࡏ͢Δ
͋·Γ ΘΕͳ͍ ͭ
Responder Chain (in UIKit) •Ԟ·ͬͨ֊ͰൃՐ •֊ͷͲ͜Ͱड͚ΒΕΔ •DelegateͷΑ͏ʹreceiverΛड͚ ͢ඞཁ͕ͳ͍ •ଞखஈͰಉ͡ߏΛ࣮ݱ͢Δͷ ͍͠
Responder Chain (in UIKit) UITabBarController UIViewController UITabBarController UIViewController UIViewController UIViewController
UITableView UITableViewCell UIView UIButton
sendActionͷμϝͳͱ͜Ζ •ΠϯλʔϑΣʔε͕ݹ͍ •Selector(จࣈྻ)ࢦఆ •ܾ·ͬͨҾͷϝιουͰ͔͠௨Λड͚ΒΕ ͳ͍ •ಛఆͷObjectΛͮ͠Β͍
sendAction + Rx // HogeΛࢹ self.rx_action(Int.self, .Hoge) .subscribeNext { NSLog("context:
\($0 ?? 0)") //< 100 } //.addDisposableTo(disposeBag) // HogeΛൃՐ self.sendActionForKey(.Hoge, context: 100)
·ͱΊ •ϨΨγʔͳϓϩδΣΫτʹϦΞΫςΟϒͳཁૉ ΛऔΓೖΕΔͷҙ֎ͱ؆୯ˍҰఆҎ্ͷޮՌ ͕ݟࠐΊΔ •KVOͳͲݹ͖ྑ͖Έ߹ʹΑͬͯ༗༻ ͳબࢶ
Χοίѱ͘ ϦΞΫςΟϒ ͡ΌμϝͰ͔͢ʁ μϝ͡Όͳ͍ͬ!!
More Information Yuta ToKoRo iOS App Programer Twitter @tokorom http://www.tokoro.me/