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

Scott Gardner: Reactive Programming with RxSwift

Realm
June 15, 2016

Scott Gardner: Reactive Programming with RxSwift

Realm

June 15, 2016
Tweet

More Decks by Realm

Other Decks in Programming

Transcript

  1. What is Reactive Programming? Reactive Programming is essentially about modeling

    asynchronous sequences So what is RxSwift? RxSwift implements patterns, types, and operators to create and work with Observable sequences
  2. REACTIVE EXTENSIONS N ov. ‘09 Rx.N ET RxJS M ar.

    ‘10 RxJava M ar. ‘12 RxC pp N ov. ‘12 RxRuby D ec. ‘12 RxScala, RxC lojure, RxG roovy, RxJRuby Jan. ‘13 RxPY, RxPH P M ar. ‘13 RxKotlin O ct. ‘13 RxSw ift Feb. ‘15
  3. ABOUT RX ▸ Event-driven ▸ Asynchronous ▸ Functional ▸ Common

    patterns ▸ Observer ▸ Iterator ▸ Cross-platform
  4. LIFECYCLE OF AN OBSERVABLE SEQUENCE ▸ Next ▸ Error ▸

    Completed ▸ dispose() / DisposeBag 1 2 3
  5. SETUP //: Please build the scheme 'RxSwiftPlayground' first import XCPlayground

    XCPlaygroundPage.currentPage.needsIndefiniteExecution = true import RxSwift func exampleOf(description: String, action: Void -> Void) { print("\n--- Example of:", description, "---") action() }
  6. --- Example of: just --- Next(32) Completed CREATING & SUBSCRIBING

    exampleOf("just") { Observable.just(32) } .subscribe { } print($0)
  7. --- Example of: just --- Next(32) Completed exampleOf("just") { Observable.just(32)

    .subscribe { element in print(element) } } CREATING & SUBSCRIBING
  8. --- Example of: just --- 32 exampleOf("just") { _ =

    Observable.just(32) .subscribeNext { print($0) } } CREATING & SUBSCRIBING
  9. --- Example of: of --- 1 2 3 4 5

    CREATING & SUBSCRIBING exampleOf("of") { Observable.of(1, 2, 3, 4, 5) } .subscribeNext { print($0) } .dispose()
  10. exampleOf("toObservable") { } --- Example of: toObservable --- 1 2

    3 CREATING & SUBSCRIBING let disposeBag = DisposeBag() [1, 2, 3].toObservable() .subscribeNext { print($0) } .addDisposableTo(disposeBag)
  11. CREATING & SUBSCRIBING let string = BehaviorSubject(value: "Hello") string.subscribe {

    print($0) } .addDisposableTo(disposeBag) exampleOf("BehaviorSubject") { let disposeBag = DisposeBag() } string.on(.Next("World!"))
  12. CREATING & SUBSCRIBING let string = BehaviorSubject(value: "Hello") string.subscribe {

    print($0) } .addDisposableTo(disposeBag) string.onNext("World!") exampleOf("BehaviorSubject") { let disposeBag = DisposeBag() }
  13. --- Example of: BehaviorSubject --- Next(Hello) Next(World!) CREATING & SUBSCRIBING

    let string = BehaviorSubject(value: "Hello") string.subscribe { print($0) } .addDisposableTo(disposeBag) string.onNext("World!") exampleOf("BehaviorSubject") { let disposeBag = DisposeBag() }
  14. CREATING & SUBSCRIBING --- Example of: Variable --- Next(1) Next(12)

    Next(1234567) Completed number.value = 12 number.value = 1_234_567 exampleOf("Variable") { let disposeBag = DisposeBag() let number = Variable(1) number.asObservable() .subscribe { print($0) } .addDisposableTo(disposeBag) }
  15. TRANSFORMING --- Example of: map --- 1 4 9 Observable.of(1,

    2, 3) example("map") { let disposeBag = DisposeBag() } .map { $0 * $0 } .subscribeNext { print($0) } .addDisposableTo(disposeBag)
  16. TRANSFORMING --- Example of: flatMap --- Scott Lori Eric exampleOf("flatMap")

    { let disposeBag = DisposeBag() } struct Person { var name: Variable<String> } let scott = Person(name: Variable("Scott")) let lori = Person(name: Variable("Lori")) let person = Variable(scott) person.asObservable() .flatMap { $0.name.asObservable() } .subscribeNext { print($0) } .addDisposableTo(disposeBag) person.value = lori scott.name.value = "Eric"
  17. TRANSFORMING --- Example of: flatMapLatest --- Scott Lori exampleOf("flatMapLatest") {

    let disposeBag = DisposeBag() struct Person { var name: Variable<String> } let scott = Person(name: Variable("Scott")) let lori = Person(name: Variable("Lori")) let person = Variable(scott) person.asObservable() .flatMapLatest { $0.name.asObservable() } .subscribeNext { print($0) } .addDisposableTo(disposeBag) person.value = lori scott.name.value = "Eric" }
  18. TRANSFORMING exampleOf("flatMapLatest") { let disposeBag = DisposeBag() struct Person {

    var name: Variable<String> } let scott = Person(name: Variable("Scott")) let lori = Person(name: Variable("Lori")) let person = Variable(scott) person.asObservable() .debug("person") .flatMapLatest { $0.name.asObservable() } .subscribeNext { print($0) } .addDisposableTo(disposeBag) person.value = lori scott.name.value = "Eric" } --- Example of: flatMapLatest --- 2016-05-28 07:31:22.555: person -> subscribed 2016-05-28 07:31:22.556: person -> Event Next((Person #1)(nam...able<Swift.String>)) Scott 2016-05-28 07:31:22.557: person -> Event Next((Person #1)(nam...able<Swift.String>)) Lori 2016-05-28 07:31:22.560: person -> Event Completed 2016-05-28 07:31:22.560: person -> disposed
  19. RXSWIFT OPERATORS ▸ Creating asObservable create deferred empty error toObservable

    interval never just of range repeatElement timer ▸ Transforming buffer flatMap flatMapFirst flatMapLatest map scan window ▸ Filtering debounce / throttle distinctUntilChanged elementAt filter sample skip take takeLast single ▸ Conditional & Boolean amb skipWhile skipUntil takeUntil takeWhile ▸ Mathematical & Aggregate concat reduce toArray ▸ Connectable multicast publish refCount replay shareReplay ▸ Combining merge startWith switchLatest combineLatest zip ▸ Error Handling catch retry retryWhen ▸ Observing delaySubscription do / doOnNext observeOn subscribe subscribeOn timeout using debug
  20. exampleOf("distinctUntilChanged") { let disposeBag = DisposeBag() let searchString = Variable("iOS")

    searchString.asObservable() .map { $0.lowercaseString } .distinctUntilChanged() .subscribeNext { print($0) } .addDisposableTo(disposeBag) searchString.value = "IOS" searchString.value = "Rx" searchString.value = "ios" } FILTERING --- Example of: distinctUntilChanged --- ios rx ios
  21. exampleOf("combineLatest") { let disposeBag = DisposeBag() let number = PublishSubject<Int>()

    let string = PublishSubject<String>() Observable.combineLatest(number, string) { "\($0) \($1)" } .subscribeNext { print($0) } .addDisposableTo(disposeBag) number.onNext(1) print("Nothing yet") string.onNext("A") number.onNext(2) string.onNext("B") string.onNext("C") } COMBINING --- Example of: combineLatest --- Nothing yet 1 A 2 A 2 B 2 C
  22. exampleOf("takeWhile") { [1, 2, 3, 4, 5, 6, 5, 4,

    3, 2, 1].toObservable() .takeWhile { $0 < 5 } .subscribeNext { print($0) } .dispose() } CONDITIONAL --- Example of: takeWhile --- 1 2 3 4
  23. MATHEMATICAL --- Example of: _scan --- 11 13 16 20

    25 exampleOf("scan") { Observable.of(1, 2, 3, 4, 5) .subscribeNext { print($0) } .dispose() } .scan(10, accumulator: +)
  24. MATHEMATICAL --- Example of: _scan --- 11 13 16 20

    25 exampleOf("scan") { Observable.of(1, 2, 3, 4, 5) .subscribeNext { print($0) } .dispose() } .scan(10) { $0 + $1 }
  25. ERROR HANDLING --- Example of: error --- A exampleOf("error") {

    enum Error: ErrorType { case A } Observable<Int>.error(Error.A) .subscribeError { // Handle error print($0) } .dispose() }
  26. ▸ github.com/ReactiveX/RxSwift ▸ reactivex.io ▸ rxmarbles.com ▸ slack.rxswift.org ▸ rx-marin.com

    ▸ as.ync.io ▸ github.com/scotteg/RxSwiftPlayer ▸ github.com/Artsy/eidolon ▸ lynda.com/Scott-Gardner/281956-1.html WANT MORE RXSWIFT?