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

Introduction to RxJava on Android

Introduction to RxJava on Android

Talk about RxJava basics and a coding exercise of using RxJava with Retrofit towards the end.


Chris Arriola

June 02, 2016

More Decks by Chris Arriola

Other Decks in Programming


  1. Intro to RxJava on Android Chris Arriola Functional Reactive Programming

  2. • Makes dealing with concurrency easy • Makes code a

    lot more concise and readable • Encourages defensive programming and makes error handling easy • Increases the level of abstraction Why consider RxJava?
  3. Nested Network Call w/o RxJava

  4. Nested Network Call w/ RxJava

  5. What is RxJava? • Java implementation of .NET’s Reactive Extensions

    ◦ Specification for Functional Reactive Programming (FRP) • Programming with asynchronous data streams ◦ Not a new concept. Think: click events/handlers, event bus, etc. ◦ Reactive part • Can combine, create, filter, map, or transform any stream ◦ Functional part
  6. Core RxJava Constructs • Observable • Observer • Operator

  7. • Emits items in a sequence ◦ Like an Iterator,

    it produces all items in a sequence • Each emitted item will be propagated to each Observer Observable
  8. Observer • Observer (aka the “subscriber”) subscribes to Observable •

    Observers are notified of a new item through #onNext(...) • Observers are notified when there sequence completes/fails through #onCompleted()/#onError()
  9. Creating an Observable • Create an Observable using #create(...)

  10. Creating an Observable • Create an Observable from item/s using

    #just(...) • Create an Observable from an Iterable using #from(...) • Create an Observable that emits items given an interval using #interval(...)
  11. Creating an Observable (cont’d) • Defer creation of Observable until

  12. Subscribing to an Observable • Observer has #onNext(...), #onCompleted(...), and

  13. Unsubscribing to an Observable • Observable#subscribe(...) returns a Subscription object

    from which the caller can invoke Subscription#unsubscribe()
  14. Operator • Most powerful part about Observables is that you

    can transform them and perform functional style programming—map(), debounce(), filter(), etc. • Transform Observable instances through Operators
  15. Operator

  16. Transforming an Observable • Transform values emitted by Observable using

    the #map(...) operator
  17. Filtering an Observable • Filter values emitted by Observable using

    #filter(...) operator
  18. Scheduling an Observable • Specify a Scheduler where the Observable

    should operate using #subscribeOn(...), specify a Scheduler where the Observable should notify its observers using #observeOn(...)
  19. Error Handling • Re-subscribe/retry when the Observable emits an error

  20. Ex. Double Clicks • Stream of clicks • Accumulate clicks

    in a list • Get length of list • Filter
  21. Ex. Double Clicks Simulate a stream of clicks:

  22. Ex. Double Clicks Accumulate clicks until 250 ms has passed

    between clicks:
  23. Ex. Double Clicks Map the length of each list &

    filter for sizes >= 2:
  24. None
  25. GitHub Example • Interact with GitHub’s API using Retrofit and

    RxJava ◦ https://github.com/arriolac/GithubRxJava
  26. Questions??

  27. Thank You! Twitter: @arriolachris