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

5b42fb9892c0404457899919d583e087?s=47 Orakaro
September 15, 2016

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

歌舞伎座.tech#11

5b42fb9892c0404457899919d583e087?s=128

Orakaro

September 15, 2016
Tweet

Transcript

  1. The introduction to RxSwift you've been missing @dtvd88 Վ෣ب࠲.tech#11

  2. ࣗݾ঺հ • 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)
  3. ࠓ೔ͷ಺༰ • RP(Reactive Programming)ͱ͸ • RxSwiftͱ͸ • The introduction to

    RxSwift you've been missing (͋ͳ͕ͨٻΊ͍ͯͨRxSwiftೖ໳)
  4. Reactive Programming http://reactivex.io/

  5. ඇಉظσʔλετϦʔϜΛ༻͍Δϓϩάϥϛϯά ObserverύλʔϯͱIteratorύλʔϯͱ ؔ਺ܕϓϩάϥϛϯά

  6. ετϦʔϜΛ࡞੒ɺม׵ɺ߹੒͢Δૢ࡞ؔ਺ • ετϦʔϜ͸ෆมੑΛ࣋ͭ • ૢ࡞ؔ਺͸νΣʔϯͰ͖Δ

  7. 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
  8. RxSwiftͱ͸

  9. Swift൛Rxͷ࣮૷ϥΠϒϥϦ • ϝΠϯϨϙ͸ReactiveXʹ഑ଐ: https://github.com/ ReactiveX/RxSwift • ࣗ෼ͰผͷίϛϡχςΟΛ͍࣋ͬͯΔɿhttps://github.com/ RxSwiftCommunity • RxSwiftͷษڧ

    = (RxSwift + RxCocoa + ίϛϡχ ςΟϥΠϒϥϦʔ)ͷษڧ
  10. RxSwiftͰRealtime Github Search

  11. 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)
  12. RxSwiftΛษڧ͢Δʹ͸

  13. The introduction to Reactive Programming you've been missing • @andrestaltz͞Μͷૉ੖Β͍͠ೖ໳ΨΠυ

    • Twitterͷ"Who to follow" ਪનϘοΫεΛ࢖ͬͯReactive ProgrammingΛղઆ͢Δ • ͋ͳ͕ͨٻΊ͍ͯͨϦΞΫςΟϒϓϩάϥϛϯάೖ໳
  14. 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
  15. 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]-------|->
  16. 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]-------|->
  17. map ͱ flatMap let responseStream = requestStream.map { url in

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

    return UserModel().findUsers(url) // Observable<[User]> } // Observable<[User]>
  19. શ͕ͯετϦʔϜʹͰ͖Δ

  20. 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---->
  21. 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-------->
  22. ৄ͘͠͸Githubʹ͝ཡ͍ͩ͘͞!