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

カッコ悪くリアクティブじゃダメですか?

tokorom
April 13, 2016

 カッコ悪くリアクティブじゃダメですか?

レガシーな既存プロジェクトへのRxSwift導入事例

tokorom

April 13, 2016
Tweet

More Decks by tokorom

Other Decks in Technology

Transcript

  1. 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ʂ
  2. topic.rx_observe(Bool.self, "isHearted") .subscribeNext { [weak self] in self?.heartButton?.selected = $0

    ?? false } //.addDisposableTo(disposeBag) Modelͷߋ৽Λ؂ࢹ •؂ࢹଆαϯϓϧʢViewʣ ͑ͬ!? ViewModel ͸?
  3. ௨৴઀ଓ෮׆͞Μ໰୊ •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() }
  4. มΘ͍ͬͯ͘͜ͱ •ઈࢍਐԽத •ReactKit / SwiftBond / RxSwift •The Evolution of

    Flux Framework (2015೥5݄ʣ •SwiftFlux / Swift Flow / ReduxKit / ReSwift •̍೥ޙʹ͸·ͩݟ͵ϥΠϒϥϦ࢖ͬͯΔʁ •RxSwift͸ྑ͍ײ͡ʹރΕ͖ͯͯΔ͔΋
  5. sendAction + Rx // HogeΛ؂ࢹ self.rx_action(Int.self, .Hoge) .subscribeNext { NSLog("context:

    \($0 ?? 0)") //< 100 } //.addDisposableTo(disposeBag) // HogeΛൃՐ self.sendActionForKey(.Hoge, context: 100)