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

RxJava in Baby Steps

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

Annyce D.

October 31, 2017
Tweet

More Decks by Annyce D.

Other Decks in Programming

Transcript

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

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

    all of the above Which of the following is an asynchronous data stream?
  3. Data Transformati on Observable.just(5, 6, 7) .map { ";-) ".repeat(it)

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

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

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

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

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

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

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

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

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

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

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

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

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

    30 35 listOf(5, 6, 7) .asSequence() .map { it * 5 } .filter { it > 25 } .toList()
  17. RxJava numbers .map { it * 5 } .filter {

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

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

    it > 25 } .subscribe() 5 6 map filter 25 30 30 35 7 35 35
  20. A: emit items B: be cold C: be hot D:

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

    all of the above Observables can. . .
  22. 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") } }
  23. A: have a lifecycle B: always complete C: Never Error

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

    C: Never Error D: all of the above
  25. Operator: map() ;-) ;-) ;-) ;-) ;-) ;-) ;-) ;-)

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

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

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

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

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

    return ";-) ".repeat(t) } }) .map { ";-) ".repeat(it) }
  31. 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?
  32. 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?
  33. what’s the output? Observable.just(1, 2, 3) .map { it *

    2 } .filter { it < 6 } .subscribe { println(it) }
  34. 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) }
  35. 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
  36. Operator: flatmap() val users // Observable<User> val posts: Observable<Post> posts

    = users.flatMap { getUsersPosts(it.id) } posts.subscribe { println(it) }
  37. should you use rxjava? like functi onal programing? Process items

    asynchronously? compose data? handle errors gracefully?
  38. you

  39. • 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