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

5b42fb9892c0404457899919d583e087?s=47 Orakaro
September 19, 2016

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

Presentation at Kabukiza Tech #11

5b42fb9892c0404457899919d583e087?s=128

Orakaro

September 19, 2016
Tweet

Transcript

  1. The introduction to RxSwift you've been missing @dtvd88 Kabukiza.tech#11

  2. Who am I • Vu Nhat Minh (@dtvd88) • Author

    of RainbowStream • Scala/Swift engineer • Swift Monad: Proof of concept • The introduction to RxSwift you've been missing • JLPT Free Quiz (~10000 DL)
  3. Agenda • RP(Reactive Programming) • RxSwift • The introduction to

    RxSwift you've been missing
  4. Reactive Programming http://reactivex.io/

  5. Programming with asynchronous data streams Observer pattern / Iterator pattern

    / Functional Programming
  6. Functions to combine, create and filter streams • Stream is

    immutable • Function can be chained
  7. ReactiveExtension(Rx) libraries 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. Rx library for Swift • Member of ReactiveX: https://github.com/ ReactiveX/RxSwift

    • Communityɿhttps://github.com/RxSwiftCommunity • Learning RxSwift = Learning (RxSwift + RxCocoa + community libraries)
  10. Realtime Github Search using RxSwift

  11. Realtime Github Search using RxSwift 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. Learning RxSwift

  13. The introduction to Reactive Programming you've been missing • Incredibly

    well-written tutorial by @andrestaltz • Use Twitter’s "Who to follow" box to explain Reactive Programming
  14. The introduction to RxSwift you've been missing • Use Twitter’s

    "Who to follow" box • Code in Swift (RxSwift + RxCocoa) • Use Moya/RxSwift for networking part • https://github.com/DTVD/The-introduction-to-RxSwift-you-have-been- missing
  15. Call API and build 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. Call API and build 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 vs flatMap let responseStream = requestStream.map { url in

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

    return UserModel().findUsers(url) // Observable<[User]> } // Observable<[User]>
  19. Everything is a stream

  20. Observable<User?> Stream type 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. Data binding for 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. See details in Github