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
520
SharePlayの歴史と進化 そしてvisionOSへ (iOSDC 2023)
tokorom
2
870
5分でSharePlay入門
tokorom
2
1.4k
HomeKit 2020
tokorom
7
2.4k
Advanced Segue 2019年のSegue事情
tokorom
9
5.8k
tvOSアプリUIの勘所
tokorom
1
1.9k
古き良きsendAction (in potatotips #26)
tokorom
1
3.5k
画面遷移と私(iOS)
tokorom
4
3.7k
長生きするために心臓に悪いリリースはもうやめよう
tokorom
21
33k
Other Decks in Technology
See All in Technology
独自ツール開発でスタジオ撮影をDX!「VLS(Virtual LED Studio)」 / dx-studio-vls
cyberagentdevelopers
PRO
1
180
生成AIの強みと弱みを理解して、生成AIがもたらすパワーをプロダクトの価値へ繋げるために実践したこと / advance-ai-generating
cyberagentdevelopers
PRO
1
180
プロダクトエンジニアが活躍する環境を作りたくて 事業責任者になった話 ~プロダクトエンジニアの行き着く先~
gimupop
1
460
プロダクト成長に対応するプラットフォーム戦略:Authleteによる共通認証基盤の移行事例 / Building an authentication platform using Authlete and AWS
kakehashi
1
150
[AWS JAPAN 生成AIハッカソン] Dialog の紹介
yoshimi0227
0
150
事業者間調整の行間を読む 調整の具体事例
sugiim
0
1.3k
omakaseしないための.rubocop.yml のつくりかた / How to Build Your .rubocop.yml to Avoid Omakase #kaigionrails
linkers_tech
3
730
よくわからんサービスについての問い合わせが来たときの強い味方 Amazon Q について
kazzpapa3
0
220
【若手エンジニア応援LT会】AWS Security Hubの活用に苦労した話
kazushi_ohata
0
160
Product Engineer Night #6プロダクトエンジニアを育む仕組み・施策
hacomono
PRO
1
460
Amazon FSx for NetApp ONTAPを利用するにあたっての要件整理と設計のポイント
non97
1
160
最速最小からはじめるデータプロダクト / Data Product MVP
amaotone
5
730
Featured
See All Featured
Thoughts on Productivity
jonyablonski
67
4.3k
Fantastic passwords and where to find them - at NoRuKo
philnash
50
2.8k
Making Projects Easy
brettharned
115
5.9k
Building Applications with DynamoDB
mza
90
6.1k
Sharpening the Axe: The Primacy of Toolmaking
bcantrill
37
1.8k
The Cult of Friendly URLs
andyhume
78
6k
How STYLIGHT went responsive
nonsquared
95
5.2k
Gamification - CAS2011
davidbonilla
80
5k
Git: the NoSQL Database
bkeepers
PRO
425
64k
Dealing with People You Can't Stand - Big Design 2015
cassininazir
364
22k
"I'm Feeling Lucky" - Building Great Search Experiences for Today's Users (#IAC19)
danielanewman
226
22k
Responsive Adventures: Dirty Tricks From The Dark Corners of Front-End
smashingmag
250
21k
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/