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
lightweight introducing with reactive cocoa
Search
Sponsored
·
Your Podcast. Everywhere. Effortlessly.
Share. Educate. Inspire. Entertain. You do you. We'll handle the rest.
→
Hiroki Kumamoto
June 25, 2015
0
62
lightweight introducing with reactive cocoa
Reactive Cocoaを自作iOSに組みこんだときのtips
Hiroki Kumamoto
June 25, 2015
Tweet
Share
More Decks by Hiroki Kumamoto
See All by Hiroki Kumamoto
How to implement timetable view on iOS
kumabook
0
110
Promiseを実装してみよう
kumabook
0
330
bff_tips.pdf
kumabook
0
95
Tips of server-side rendering with react + redux
kumabook
0
88
プロトコルを使って複数サービスを跨いだmusic playerを実装する
kumabook
2
650
emacs helm likeな WebExtensions を作った話
kumabook
0
170
merges sort
kumabook
0
81
basic_sort
kumabook
0
74
git_peco_tips
kumabook
0
33
Featured
See All Featured
Building a Modern Day E-commerce SEO Strategy
aleyda
45
8.7k
Money Talks: Using Revenue to Get Sh*t Done
nikkihalliwell
0
150
It's Worth the Effort
3n
188
29k
Exploring the relationship between traditional SERPs and Gen AI search
raygrieselhuber
PRO
2
3.6k
Rebuilding a faster, lazier Slack
samanthasiow
85
9.4k
What the history of the web can teach us about the future of AI
inesmontani
PRO
1
430
Testing 201, or: Great Expectations
jmmastey
46
8k
Refactoring Trust on Your Teams (GOTO; Chicago 2020)
rmw
35
3.4k
No one is an island. Learnings from fostering a developers community.
thoeni
21
3.6k
VelocityConf: Rendering Performance Case Studies
addyosmani
333
24k
Introduction to Domain-Driven Design and Collaborative software design
baasie
1
590
Visual Storytelling: How to be a Superhuman Communicator
reverentgeek
2
430
Transcript
Reactive Cocoa Λ SwiftͰ ΊͪΌϥΠτʹಋೖͯ͠Έͨ ͳ͠ 2015/06/16 potato tips #18
Hiroki Kumamoto (@kumabook)
Who am I ? • Hiroki Kumamoto • @kumabook •
ษڧձॳࢀՃʂ • झຯͰΞϓϦͭͬͯ͘·͢ • Իָ༻RSSϦʔμ iOSΞϓϦ MusicFav • https://github.com/kumabook/MusicFav/ • Feedly Cloud API libraryҰॹʹ࡞ • https://github.com/kumabook/FeedlyKit
ΊͪΌϥΠτ? • FRPతʹΘͣϥΠϒϥϦతʹ • ͦͷ̍ɿ௨৴ॲཧΛϓϩϛε෩ʹ • ͦͷ̎ɿObserver pattern (not binding)
ͦͷ1ɿϓϩϛε෩ʹ͔ͭ͏ • AFNetworkingΛϥοϓ͢Δ • callback ελΠϧ͔Β ϓϩϛε෩ʹ • SignalProducer (چColdSignal)
func getAllBlogs(#onSuccess: [Entry] -> Void, onFailure: NSError -> Void) ->
Request func getAllBlogs() -> SignalProducer<[Entry], NSError>
func getAllBlogs() -> SignalProducer<[Blog], NSError> { return SignalProducer { (sink,
disposable) in let manager = AFHTTPRequestOperationManager() let url = String(format: "%@/get_all_blogs", self.baseUrl + self.apiRoot) let operation = manager.GET(url, parameters: [:], success: { (operation:AFHTTPRequestOperation!, response:AnyObject!) -> Void in sink.put(.Next(Box(JSON(response).arrayValue.map({ Blog(json: $0) })))) sink.put(.Completed) }, failure: { (operation:AFHTTPRequestOperation!, error:NSError!) -> Void in sink.put(.Error(Box(error))) }) disposable.addDisposable { operation.cancel() } }
func start(error: (E -> ())? = default, completed: (() ->
())? = default, interrupted: (() -> ())? = default, next: (T -> ())? = default) -> Disposable fetchAllBlogs().start(next: { blogs in doSomething(blogs) }, error: {e in … }, completed: { … })
͍͚ͦ͏ʂ ͏গ͠౿ΈࠐΜͰ ͬͯΈΑ͏ ˠ when తͳͭ
• ෳͷॲཧ͕ऴΘͬͨޙʹҰ͚ͩॲཧΛ͢Δ • SignalProducer#concatͰ݁߹Ͱ͖Δ • Array#reduceͱΈ߹ΘͤΕ ͍͍ײ͡ʹͰ͖ͦ͏ • [SignalProducer<T, E>]
͔Β SignalProducer<T, E>ม͢Δ
var signal = signals.reduce(SignalProducer<Entry, NSError>.empty, combine: { (currentSignal, nextSignal) in
currentSignal |> concat(nextSignal) }) • ॳظ SignalProducer<Entry,NSError>.empty •Next が複数回きて最後にCompletedがくる
• ͜ͷล͔ΒͩΜͩΜָ͘͠ͳͬͯ͘Δ • ͱΓ͋͑ͣͳΜͰSignalProducerΛฦͨ͘͠ͳΔ ˠܕΤϥʔʹ·͞ΕΔ
SignalProducer methods • SignalProducerΛอͬͨ··Λม͍͑ͨ ˠɹ|> map Λ͏ • ೖΕࢠʹͳͬͯ͠·ͬͨ ˠ
|> flatten Λ͏ • unittest ͱ͔ Carthageͷιʔεͱ͔͕ࢀߟʹͳΔ
Observe Binding • Signal (چHotSignal) • Data binding ͱ͍͏ΑΓ
Observerύλʔϯతʹ༻
ViewModel observe observe put Event enum Event { }
• ͍ͬͯΔ͜ͱNSNotificationCenterͱ͔ͱมΘΒͳ͍͕ ܕνΣοΫ͕ޮ͘ͷ͕҆৺ײ͋Δ • observeͨ͠ͷdispose͢Δ • deinitΛ௨͍ͬͯΔ͔Ͱmemory leakνΣοΫ • Dead
lock ͨ͠߹ • b _NSLockErrorͰϒϨΠΫϙΠϯτுΔ • RealmͷDAOͷϝιουͰEventΛput
·ͱΊ • SignalProducerͱSignalΛத৺ʹϥΠτʹಋೖ • SignalProducerϓϩϛεతʹ • SignalObserverతʹ • Action, Property,
Binding (<~)FRPΛษڧͯ͠ ঃʑʹಋೖ͍͖͍ͯͨ͠
͋Γ͕ͱ͏͍͟͝·ͨ͠