Upgrade to Pro — share decks privately, control downloads, hide ads and more …

lightweight introducing with reactive cocoa

Hiroki Kumamoto
June 25, 2015
49

lightweight introducing with reactive cocoa

Reactive Cocoaを自作iOSに組みこんだときのtips

Hiroki Kumamoto

June 25, 2015
Tweet

Transcript

  1. Who am I ? • Hiroki Kumamoto • @kumabook •

    ษڧձॳࢀՃʂ • झຯͰΞϓϦͭͬͯ͘·͢ • Իָ༻RSSϦʔμ iOSΞϓϦ MusicFav • https://github.com/kumabook/MusicFav/ • Feedly Cloud API library΋Ұॹʹ࡞੒ • https://github.com/kumabook/FeedlyKit
  2. func getAllBlogs(#onSuccess: [Entry] -> Void, onFailure: NSError -> Void) ->

    Request func getAllBlogs() -> SignalProducer<[Entry], NSError>
  3. 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() } }
  4. func start(error: (E -> ())? = default, completed: (() ->

    ())? = default, interrupted: (() -> ())? = default, next: (T -> ())? = default) -> Disposable fetchAllBlogs().start(next: { blogs in doSomething(blogs) }, error: {e in … }, completed: { … })
  5. var signal = signals.reduce(SignalProducer<Entry, NSError>.empty, combine: { (currentSignal, nextSignal) in

    currentSignal |> concat(nextSignal) }) • ॳظ஋͸
 SignalProducer<Entry,NSError>.empty •Next が複数回きて最後にCompletedがくる