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

The introduction to RxSwift you've been missing...

Orakaro
September 15, 2016

The introduction to RxSwift you've been missing (Japanese)

歌舞伎座.tech#11

Orakaro

September 15, 2016
Tweet

More Decks by Orakaro

Other Decks in Programming

Transcript

  1. ࣗݾ঺հ • Vu Nhat Minh (@dtvd88) • RainbowStream࡞ऀ • ݱࡏ͸ScalaͱSwiftΤϯδχΞ

    • Swift Monad: Proof of concept • The introduction to RxSwift you've been missing • JLPT Free Quiz (~1ສ DL)
  2. ࠓ೔ͷ಺༰ • RP(Reactive Programming)ͱ͸ • RxSwiftͱ͸ • The introduction to

    RxSwift you've been missing (͋ͳ͕ͨٻΊ͍ͯͨRxSwiftೖ໳)
  3. ReactiveExtension(Rx)ͷ࣮૷ϥΠάϥϦ Languages • Java: RxJava • JavaScript: RxJS • C#:

    Rx.NET • C#(Unity): UniRx • Scala: RxScala • Clojure: RxClojure • C++: RxCpp • Ruby: Rx.rb • Python: RxPY • Groovy: RxGroovy • JRuby: RxJRuby • Kotlin: RxKotlin • Swift: RxSwift • PHP: RxPHP Frameworks • RxNetty • RxAndroid • RxCocoa
  4. RxSwiftͰRealtime Github Search let searchResults = searchBar.rx.text .throttle(0.3, scheduler: MainScheduler.instance)

    .distinctUntilChanged() .flatMapLatest { query -> Observable<[Repository]> in if query.isEmpty { return Observable.just([]) } return searchGitHub(query) .catchErrorJustReturn([]) } .observeOn(MainScheduler.instance)
  5. The introduction to Reactive Programming you've been missing • @andrestaltz͞Μͷૉ੖Β͍͠ೖ໳ΨΠυ

    • Twitterͷ"Who to follow" ਪનϘοΫεΛ࢖ͬͯReactive ProgrammingΛղઆ͢Δ • ͋ͳ͕ͨٻΊ͍ͯͨϦΞΫςΟϒϓϩάϥϛϯάೖ໳
  6. The introduction to RxSwift you've been missing • Twitterͷ"Who to

    follow" ਪનϘοΫεΛ࢖͏ • αϯϓϧίʔυ͸Swift (RxSwift + RxCocoa) • ௨৴෦෼͸Moya/RxSwift • https://github.com/DTVD/The-introduction-to-RxSwift-you-have-been- missing
  7. APIΛݺͼग़ͯ͠responseStreamΛ࡞੒ let refreshStream = refresh.rx_tap.startWith(()) let requestStream: Observable<String> = refreshStream.map

    { _ in let random = Array(1...1000).random() return “https://api.github.com/users/" + String(random) } let responseStream = requestStream.flatMap { url in return UserModel().findUsers(url) } refreshStream: --tap-------tap----------tap------------|-> requestStream: --url-------url----------url------------|-> responseStream: -----[User]-----[User]-----[User]-------|->
  8. APIΛݺͼग़ͯ͠responseStreamΛ࡞੒ let refreshStream = refresh.rx_tap.startWith(()) let requestStream: Observable<String> = refreshStream.map

    { _ in let random = Array(1...1000).random() return “https://api.github.com/users/" + String(random)ɹ// String }ɹ// Observable<String> let responseStream = requestStream.flatMap { url in return UserModel().findUsers(url) // Observable<[User]> }ɹ// Observable<[User]> refreshStream: --tap-------tap----------tap------------|-> requestStream: --url-------url----------url------------|-> responseStream: -----[User]-----[User]-----[User]-------|->
  9. map ͱ flatMap let responseStream = requestStream.map { url in

    return UserModel().findUsers(url) // Observable<[User]> } // Observable<Observable<[User]>>
  10. map ͱ flatMap let responseStream = requestStream.flatMap { url in

    return UserModel().findUsers(url) // Observable<[User]> } // Observable<[User]>
  11. Observable<User?>Λฦ͢ετϦʔϜ let closeStream = cell.cancel.rx_tap.startWith(()) let userStream: Observable<User?> = Observable.combineLatest(closeStream,

    responseStream){ (_, users) in guard users.count > 0 else {return nil} return users.random() } responseStream: --a-----------e--------i--------> closeStream: -----b----c--------d-------q----> vvvvvvvv combineLatest(f) vvvvvvv userStream: ----AB---AC--EC---ED--ID--IQ---->
  12. CellͷσʔλʹόΠσΟϯά let nilOnRefreshTapStream: Observable<User?> = refresh.rx_tap.map {_ in return nil}

    let suggestionStream = Observable.of(userStream, nilOnRefreshTapStream) .merge() .startWith(.None) suggestionStream.subscribeNext{ op in guard let u = op else { return self.clearCell(cell) } return self.setCell(cell, user: u ) }.addDisposableTo(cell.disposeBagCell) nilOnRefreshTapStream: --n-----------n--------n--------> userStream: -----u----u--------u-------u----> vvvvvvvvvvvv merge() vvvvvvvvvvvv suggestionStream: --s--s----s---s----s---s-------->