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
Hiroki Kumamoto
June 25, 2015
0
53
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
86
Promiseを実装してみよう
kumabook
0
280
bff_tips.pdf
kumabook
0
61
Tips of server-side rendering with react + redux
kumabook
0
67
プロトコルを使って複数サービスを跨いだmusic playerを実装する
kumabook
2
540
emacs helm likeな WebExtensions を作った話
kumabook
0
140
merges sort
kumabook
0
77
basic_sort
kumabook
0
70
git_peco_tips
kumabook
0
26
Featured
See All Featured
Keith and Marios Guide to Fast Websites
keithpitt
410
22k
StorybookのUI Testing Handbookを読んだ
zakiyama
28
5.4k
BBQ
matthewcrist
85
9.4k
CoffeeScript is Beautiful & I Never Want to Write Plain JavaScript Again
sstephenson
160
15k
Imperfection Machines: The Place of Print at Facebook
scottboms
267
13k
Large-scale JavaScript Application Architecture
addyosmani
510
110k
CSS Pre-Processors: Stylus, Less & Sass
bermonpainter
356
29k
Optimising Largest Contentful Paint
csswizardry
33
3k
Build your cross-platform service in a week with App Engine
jlugia
229
18k
Why Our Code Smells
bkeepers
PRO
335
57k
How GitHub (no longer) Works
holman
312
140k
Building a Modern Day E-commerce SEO Strategy
aleyda
38
7k
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Λษڧͯ͠ ঃʑʹಋೖ͍͖͍ͯͨ͠
͋Γ͕ͱ͏͍͟͝·ͨ͠