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

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

Orakaro
September 19, 2016

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

Presentation at Kabukiza Tech #11

Orakaro

September 19, 2016
Tweet

More Decks by Orakaro

Other Decks in Programming

Transcript

  1. 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)
  2. 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
  3. Rx library for Swift • Member of ReactiveX: https://github.com/ ReactiveX/RxSwift

    • Communityɿhttps://github.com/RxSwiftCommunity • Learning RxSwift = Learning (RxSwift + RxCocoa + community libraries)
  4. 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)
  5. 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
  6. 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
  7. 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]-------|->
  8. 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]-------|->
  9. map vs flatMap let responseStream = requestStream.map { url in

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

    return UserModel().findUsers(url) // Observable<[User]> } // Observable<[User]>
  11. 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---->
  12. 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-------->