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
55
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
100
Promiseを実装してみよう
kumabook
0
310
bff_tips.pdf
kumabook
0
79
Tips of server-side rendering with react + redux
kumabook
0
78
プロトコルを使って複数サービスを跨いだmusic playerを実装する
kumabook
2
580
emacs helm likeな WebExtensions を作った話
kumabook
0
160
merges sort
kumabook
0
79
basic_sort
kumabook
0
72
git_peco_tips
kumabook
0
30
Featured
See All Featured
The Straight Up "How To Draw Better" Workshop
denniskardys
236
140k
[RailsConf 2023 Opening Keynote] The Magic of Rails
eileencodes
30
9.6k
Let's Do A Bunch of Simple Stuff to Make Websites Faster
chriscoyier
507
140k
Speed Design
sergeychernyshev
32
1.1k
Reflections from 52 weeks, 52 projects
jeffersonlam
351
21k
Thoughts on Productivity
jonyablonski
69
4.8k
Templates, Plugins, & Blocks: Oh My! Creating the theme that thinks of everything
marktimemedia
31
2.5k
Building a Modern Day E-commerce SEO Strategy
aleyda
43
7.5k
Large-scale JavaScript Application Architecture
addyosmani
512
110k
CSS Pre-Processors: Stylus, Less & Sass
bermonpainter
358
30k
JavaScript: Past, Present, and Future - NDC Porto 2020
reverentgeek
50
5.5k
Docker and Python
trallard
45
3.5k
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Λษڧͯ͠ ঃʑʹಋೖ͍͖͍ͯͨ͠
͋Γ͕ͱ͏͍͟͝·ͨ͠