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

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. RXSWIFT REACTIVE PROGRAMMING WITH + =

  2. ABOUT ME

  3. ABOUT YOU ▸Swift ▸Reactive programming

  4. TRADITIONAL VS. REACTIVE A SIMPLE EXAMPLE IN IOS

  5. MODEL

  6. VIEW MODEL

  7. VIEW CONTROLLER

  8. None
  9. REACTIVE VIEW MODEL

  10. REACTIVE VIEW CONTROLLER

  11. THE RESULTS 40% SAVINGS

  12. Reactive Programming is essentially about modeling asynchronous sequences What is

    Reactive Programming?
  13. Reactive Programming is essentially about modeling asynchronous sequences

  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
  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
  16. None
  17. ABOUT RX ▸ Event-driven ▸ Asynchronous ▸ Functional ▸ Common

    patterns ▸ Observer ▸ Iterator ▸ Cross-platform
  18. LIFECYCLE OF AN OBSERVABLE SEQUENCE ▸ Next 1 2 3

  19. LIFECYCLE OF AN OBSERVABLE SEQUENCE ▸ Next tap tap tap

  20. LIFECYCLE OF AN OBSERVABLE SEQUENCE ▸ Next ▸ Error 1

    2 3
  21. LIFECYCLE OF AN OBSERVABLE SEQUENCE ▸ Next ▸ Error ▸

    Completed 1 2 3
  22. LIFECYCLE OF AN OBSERVABLE SEQUENCE ▸ Next ▸ Error ▸

    Completed ▸ dispose() / DisposeBag 1 2 3
  23. RXSWIFT ▸RxSwift ▸RxCocoa ▸RxSwiftCommunity ▸ RxDataSources

  24. SETUP ▸ Install ThisCouldBeUsButYouPlaying ▸ bit.ly/podsPlaygrounds ▸ gem install cocoapods-playgrounds

    ▸ Create RxSwiftPlayground ▸ pod playgrounds RxSwift
  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() }
  26. --- Example of: just --- Next(32) Completed CREATING & SUBSCRIBING

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

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

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

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

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

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

    print($0) } .addDisposableTo(disposeBag) string.onNext("World!") exampleOf("BehaviorSubject") { let disposeBag = DisposeBag() }
  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() }
  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) }
  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)
  36. TRANSFORMING 1 1 2 4 3 9 map { $0

    * $0 }
  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"
  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" }
  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
  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
  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
  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
  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
  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: +)
  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 }
  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() }
  47. NETWORKING

  48. NETWORKING

  49. NETWORKING

  50. NETWORKING

  51. None
  52. NETWORKING

  53. How about one more?

  54. RXCOREDATA

  55. RXCOREDATA

  56. NETWORKING

  57. How do I get started with RxSwift? I’m glad you

    asked! !
  58. LEARN RXSWIFT ▸This talk ▸RxSwift operators

  59. RXSWIFT OPERATORS

  60. LEARN RXSWIFT ▸This talk ▸RxSwift operators ▸RxCocoa ▸RxExample

  61. RXSWIFT OPERATORS

  62. RXSWIFT OPERATORS

  63. LEARN RXSWIFT ▸This talk ▸RxSwift operators ▸RxCocoa ▸RxExample ▸RxCommunity

  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?
  65. QUESTIONS?

  66. THANK YOU! Scott Gardner @scotteg scotteg.com as.ync.io bit.ly/scottOnLyndaDotCom