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

RxJava in Baby Steps

Ce5b219b385d3e316a553d8a41900ed9?s=47 Annyce D.
October 31, 2017

RxJava in Baby Steps

Reactive Programming with RxJava has widely been adopted by both backend services and Android applications alike. Yet, the steep learning curve leaves many developers hesitant about adding it to their own Software tool belt. I was one such developer. Over the past two years, I’ve watched countless videos, read numerous blog posts and attended several conference talks on the subject. Yet, I often left each experience feeling only slightly more knowledgeable, but not quite empowered to start using RxJava in my apps. That’s not going to happen in this talk!

We cover the bare minimum concepts you need to grok, in order to start using RxJava today. In particular, we focus on:
* The 3 O’s: Observable, Observer and Operator
* The most common Operators: map(), flatMap(), and filter()
* Understanding those Marble Diagrams

Ce5b219b385d3e316a553d8a41900ed9?s=128

Annyce D.

October 31, 2017
Tweet

More Decks by Annyce D.

Other Decks in Programming

Transcript

  1. None
  2. a learning curve

  3. a steep learning curve

  4. you a steep learning curve

  5. superstar a steep learning curve you

  6. you superstar a steep learning curve

  7. you superstar a steep learning curve me

  8. RxJava in baby steps @brwngrldev

  9. asynchronous data streams

  10. None
  11. GPS Updates Time

  12. GPS Updates Time -36.34543, 3.23445

  13. -36.34543, 3.23445 -36.24543, 3.23425 GPS Updates Time

  14. -36.34543, 3.23445 -36.24543, 3.23425 -35.34543, 3.13445 GPS Updates Time

  15. Time server response

  16. i want toys!!! Time server response

  17. Time server response toys

  18. Time i want toys!!! server response

  19. Time server response

  20. Which of the following is an asynchronous data stream? A:

    click events B: database access C: server response D: all of the above
  21. A: click events B: database access C: server response D:

    all of the above Which of the following is an asynchronous data stream?
  22. None
  23. None
  24. None
  25. scientific Research

  26. scientific Research

  27. None
  28. None
  29. Data Transformati on Observable.just(5, 6, 7) .map { ";-) ".repeat(it)

    } .subscribe { println(it) }
  30. Data Transformati on Observable.just(5, 6, 7) .map { ";-) ".repeat(it)

    } .subscribe { println(it) } ;-) ;-) ;-) ;-) ;-) ;-) ;-) ;-) ;-) ;-) ;-) ;-) ;-) ;-) ;-) ;-) ;-) ;-)
  31. chaining Observable.just(5, 6, 7) .map { ";-) ".repeat(it) } .filter

    { it.length < 24 } .subscribe { println(it) }
  32. chaining ;-) ;-) ;-) ;-) ;-) Observable.just(5, 6, 7) .map

    { ";-) ".repeat(it) } .filter { it.length < 24 } .subscribe { println(it) }
  33. None
  34. None
  35. RxJava is. . . ? A: the silver bullet B:

    simply magic C: pure voodoo D: a library
  36. RxJava is. . . ? A: the silver bullet B:

    simply magic C: pure voodoo D: a library
  37. listOf(5, 6, 7) .map { it * 5 } .filter

    { it > 25 } kotlin collecti ons
  38. listOf(5, 6, 7) .map { it * 5 } .filter

    { it > 25 } kotlin collecti ons 5 6 7
  39. listOf(5, 6, 7) .map { it * 5 } .filter

    { it > 25 } kotlin collecti ons 5 6 7 map 25 30 35
  40. listOf(5, 6, 7) .map { it * 5 } .filter

    { it > 25 } kotlin collecti ons 5 6 7 map 25 30 35 filter 30 35
  41. None
  42. listOf(5, 6, 7) .asSequence() .map { it * 5 }

    .filter { it > 25 } .toList() kotlin sequences
  43. kotlin sequences listOf(5, 6, 7) .asSequence() .map { it *

    5 } .filter { it > 25 } .toList() 5 6 7
  44. 5 6 7 map filter kotlin sequences 25 listOf(5, 6,

    7) .asSequence() .map { it * 5 } .filter { it > 25 } .toList()
  45. 5 6 7 map filter kotlin sequences 25 30 30

    listOf(5, 6, 7) .asSequence() .map { it * 5 } .filter { it > 25 } .toList()
  46. 5 6 7 map filter kotlin sequences 25 30 35

    30 35 listOf(5, 6, 7) .asSequence() .map { it * 5 } .filter { it > 25 } .toList()
  47. None
  48. asynchronous data streams

  49. RxJava numbers .map { it * 5 } .filter {

    it > 25 } .subscribe()
  50. RxJava numbers .map { it * 5 } .filter {

    it > 25 } .subscribe() 5 25 map filter
  51. RxJava numbers .map { it * 5 } .filter {

    it > 25 } .subscribe() 5 6 map filter 25 30 30
  52. RxJava numbers .map { it * 5 } .filter {

    it > 25 } .subscribe() 5 6 map filter 25 30 30 35 7 35 35
  53. flexible threading

  54. schedulers

  55. Observable.just(5, 6, 7) .map { ";-) ".repeat(it) } .subscribe {

    println(it) }
  56. Observable.just(5, 6, 7) .subscribeOn(Schedulers.io()) .map { ";-) ".repeat(it) } .subscribe

    { println(it) }
  57. None
  58. The Basics observable

  59. The Basics observable observer

  60. The Basics operators observable observer

  61. observable

  62. None
  63. hot observable Time

  64. hot observable Time i want hugs!!!

  65. hot observable Time hugs

  66. hot observable Time hugs hugs

  67. hot observable Time hugs hugs hugs

  68. None
  69. Cold observable Time

  70. Cold observable Time

  71. Cold observable Time

  72. Cold observable Time hugs

  73. Cold observable Time hugs hugs

  74. Cold observable Time hugs hugs hugs

  75. None
  76. where?

  77. Observable.create<Int> { subscriber -> }

  78. Observable.create<Int> { subscriber -> } Observable.just(item1, item2, item3)

  79. Observable.create<Int> { subscriber -> } Observable.just(item1, item2, item3) Observable.interval(2, TimeUnit.SECONDS)

  80. Observable.create<Int> { subscriber -> }

  81. Observable.create<Int> { subscriber -> Logger.log("create") Logger.log("complete") } Logger.log("done")

  82. Observable.create<Int> { subscriber -> Logger.log("create") subscriber.onNext(5) subscriber.onNext(6) subscriber.onNext(7) Logger.log("complete") }

    Logger.log("done")
  83. Observable.create<Int> { subscriber -> Logger.log("create") subscriber.onNext(5) subscriber.onNext(6) subscriber.onNext(7) subscriber.onComplete() Logger.log("complete")

    } Logger.log("done")
  84. None
  85. None
  86. A: emit items B: be cold C: be hot D:

    all of the above Observables can. . .
  87. A: emit items B: be cold C: be hot D:

    all of the above Observables can. . .
  88. observer

  89. interface Observer<T> { fun onError(e: Throwable) fun onComplete() fun onNext(t:

    T) fun onSubscribe(d: Disposable) }
  90. interface Observer<T> { fun onError(e: Throwable) fun onComplete() fun onNext(t:

    T) fun onSubscribe(d: Disposable) }
  91. interface Observer<T> { fun onError(e: Throwable) fun onComplete() fun onNext(t:

    T) fun onSubscribe(d: Disposable) }
  92. interface Observer<T> { fun onError(e: Throwable) fun onComplete() fun onNext(t:

    T) fun onSubscribe(d: Disposable) }
  93. interface Observer<T> { fun onError(e: Throwable) fun onComplete() fun onNext(t:

    T) fun onSubscribe(d: Disposable) }
  94. observer’s lifecycle onSubscribe

  95. observer’s lifecycle onNext Normal flow onSubscribe

  96. observer’s lifecycle onComplete Normal flow onSubscribe onNext

  97. observer’s lifecycle onComplete onError Normal flow onSubscribe onNext

  98. val observer = object : Observer<Int> { override fun onError(e:

    Throwable) { Logger.log(e) } override fun onComplete() { Logger.log("on complete") } override fun onNext(t: Int) { Logger.log("next: $t") } override fun onSubscribe(d: Disposable) { Logger.log("on subscribe") } }
  99. None
  100. interface Consumer<T> { fun accept(t: T) }

  101. val consumer = object : Consumer<Int> { override fun accept(t:

    Int) { Logger.log("next: $t") } }
  102. None
  103. None
  104. val consumer = Consumer<Int> { t -> Logger.log("next: $t") }

    obs.subscribe(consumer) consumer
  105. obs.subscribe(Consumer<Int> { t -> Logger.log("next: $t") }) consumer

  106. obs.subscribe({ t -> Logger.log("next: $t") }) consumer

  107. obs.subscribe{ t -> Logger.log("next: $t") } consumer

  108. obs.subscribe{ Logger.log("next: $it") } consumer

  109. None
  110. A: have a lifecycle B: always complete C: Never Error

    D: all of the above Observers. . .
  111. Observers. . . A: have a lifecycle B: always complete

    C: Never Error D: all of the above
  112. operator

  113. Time Operator: map()

  114. Time Operator: map() :-) :-(

  115. Time Operator: map() :-) :-) :-( :-(

  116. Time Operator: map() :-) :-) :-) :-( :-( :-(

  117. Time Operator: map() :-) :-) :-) :-) :-( :-( :-(

    :-(
  118. Operator: map() Observable.just(5, 6, 7) .map { ";-) ".repeat(it) }

    .subscribe { println(it) }
  119. Operator: map() ;-) ;-) ;-) ;-) ;-) ;-) ;-) ;-)

    ;-) ;-) ;-) ;-) ;-) ;-) ;-) ;-) ;-) ;-) Observable.just(5, 6, 7) .map { ";-) ".repeat(it) } .subscribe { println(it) }
  120. Operator: map() Observable.just(5, 6, 7) .map(object: Function<Int, String> { override

    fun apply(t: Int): String { return ";-) ".repeat(t) } }) .subscribe { println(it) }
  121. Operator: map() Observable.just(5, 6, 7) .map(object: Function<Int, String> { override

    fun apply(t: Int): String { return ";-) ".repeat(t) } }) .subscribe { println(it) }
  122. Operator: map() Observable.just(5, 6, 7) .map(object: Function<Int, String> { override

    fun apply(t: Int): String { return ";-) ".repeat(t) } }) .subscribe { println(it) }
  123. Operator: map() Observable.just(5, 6, 7) .map(object: Function<Int, String> { override

    fun apply(t: Int): String { return ";-) ".repeat(t) } }) .subscribe { println(it) }
  124. .map(object: Function<Int, String> { override fun apply(t: Int): String {

    return ";-) ".repeat(t) } }) .map { ";-) ".repeat(it) }
  125. None
  126. what’s the output? Observable.just(1, 2, 3) .map { it *

    2 } .subscribe { println(it) }
  127. A: 1, 2, 3 B: a, b, c C: 2,

    4, 6 D: 6, 2, 4 Observable.just(1, 2, 3) .map { it * 2 } .subscribe { println(it) } what’s the output?
  128. Observable.just(1, 2, 3) .map { it * 2 } .subscribe

    { println(it) } A: 1, 2, 3 B: a, b, c C: 2, 4, 6 D: 6, 2, 4 what’s the output?
  129. what’s the output? Observable.just(1, 2, 3) .map { it *

    2 } .filter { it < 6 } .subscribe { println(it) }
  130. A: 2, 3, 1 B: 2, 4 C: 1, 2,

    3 D: 6, 4 what’s the output? Observable.just(1, 2, 3) .map { it * 2 } .filter { it < 6 } .subscribe { println(it) }
  131. what’s the output? Observable.just(1, 2, 3) .map { it *

    2 } .filter { it < 6 } .subscribe { println(it) } A: 2, 3, 1 B: 2, 4 C: 1, 2, 3 D: 6, 4
  132. Operator: flatmap() via reactivex.io

  133. Operator: flatmap() via reactivex.io item

  134. Operator: flatmap() via reactivex.io item observable

  135. Operator: flatmap() Observable.just( , ) .flatMap( { Observable.just( ) }

    ) .subscribe { println(it) } :-( :-(
  136. time flatmap

  137. time flatmap :-(

  138. time flatmap :-(

  139. time flatmap :-(

  140. time flatmap :-( :-(

  141. time flatmap :-( :-(

  142. time flatmap :-( :-(

  143. time flatmap :-( :-(

  144. Long Running asynchronous

  145. Operator: flatmap() val users // Observable<User>

  146. Operator: flatmap() val users // Observable<User> val posts: Observable<Post>

  147. Operator: flatmap() val users // Observable<User> val posts: Observable<Post> posts

    = users.flatMap { getUsersPosts(it.id) }
  148. Operator: flatmap() val users // Observable<User> val posts: Observable<Post> posts

    = users.flatMap { getUsersPosts(it.id) } posts.subscribe { println(it) }
  149. via reactivex.io

  150. Flowable Maybe backpressure Disposable Single completable

  151. should you use rxjava?

  152. like functi onal programing? Process items asynchronously? compose data? handle

    errors gracefully? should you use rxjava?
  153. should you use rxjava? like functi onal programing? Process items

    asynchronously? compose data? handle errors gracefully?
  154. it depends

  155. The Basics operators observable observer

  156. you

  157. www.adavis.info @brwngrldev

  158. • Reactive Programming on Android with RxJava (http://amzn.to/2yOAkxn) • Reactive

    Programming with RxJava (http://amzn.to/2zQtqb5) • RxJava Playlist (https://goo.gl/9fw1Zv) • Learning RxJava for Android Devs (https://goo.gl/VWxFLK) • RxJava Video Course (https://caster.io/courses/rxjava) resources
  159. slide design: @lauraemilyillustrati on font: Elli ot 6, fontSquirrel.com