Scott Gardner: Reactive Programming with RxSwift

1fa9cb8c7997c8c4d3d251fb5e41f749?s=47 Realm
June 15, 2016

Scott Gardner: Reactive Programming with RxSwift

1fa9cb8c7997c8c4d3d251fb5e41f749?s=128

Realm

June 15, 2016
Tweet

Transcript

  1. 5.
  2. 8.
  3. 14.

    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
  4. 15.

    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
  5. 16.
  6. 17.

    ABOUT RX ▸ Event-driven ▸ Asynchronous ▸ Functional ▸ Common

    patterns ▸ Observer ▸ Iterator ▸ Cross-platform
  7. 22.

    LIFECYCLE OF AN OBSERVABLE SEQUENCE ▸ Next ▸ Error ▸

    Completed ▸ dispose() / DisposeBag 1 2 3
  8. 25.

    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() }
  9. 26.

    --- Example of: just --- Next(32) Completed CREATING & SUBSCRIBING

    exampleOf("just") { Observable.just(32) } .subscribe { } print($0)
  10. 27.

    --- Example of: just --- Next(32) Completed exampleOf("just") { Observable.just(32)

    .subscribe { element in print(element) } } CREATING & SUBSCRIBING
  11. 28.

    --- Example of: just --- 32 exampleOf("just") { _ =

    Observable.just(32) .subscribeNext { print($0) } } CREATING & SUBSCRIBING
  12. 29.

    --- Example of: of --- 1 2 3 4 5

    CREATING & SUBSCRIBING exampleOf("of") { Observable.of(1, 2, 3, 4, 5) } .subscribeNext { print($0) } .dispose()
  13. 30.

    exampleOf("toObservable") { } --- Example of: toObservable --- 1 2

    3 CREATING & SUBSCRIBING let disposeBag = DisposeBag() [1, 2, 3].toObservable() .subscribeNext { print($0) } .addDisposableTo(disposeBag)
  14. 31.

    CREATING & SUBSCRIBING let string = BehaviorSubject(value: "Hello") string.subscribe {

    print($0) } .addDisposableTo(disposeBag) exampleOf("BehaviorSubject") { let disposeBag = DisposeBag() } string.on(.Next("World!"))
  15. 32.

    CREATING & SUBSCRIBING let string = BehaviorSubject(value: "Hello") string.subscribe {

    print($0) } .addDisposableTo(disposeBag) string.onNext("World!") exampleOf("BehaviorSubject") { let disposeBag = DisposeBag() }
  16. 33.

    --- 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() }
  17. 34.

    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) }
  18. 35.

    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)
  19. 37.

    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"
  20. 38.

    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" }
  21. 39.

    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
  22. 40.

    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
  23. 41.

    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
  24. 42.

    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
  25. 43.

    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
  26. 44.

    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: +)
  27. 45.

    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 }
  28. 46.

    ERROR HANDLING --- Example of: error --- A exampleOf("error") {

    enum Error: ErrorType { case A } Observable<Int>.error(Error.A) .subscribeError { // Handle error print($0) } .dispose() }
  29. 51.
  30. 64.

    ▸ 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?