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
Tweet

More Decks by Chris Arriola

Other Decks in Programming

Transcript

  1. • 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?
  2. 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
  3. • 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
  4. 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()
  5. 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(...)
  6. Unsubscribing to an Observable • Observable#subscribe(...) returns a Subscription object

    from which the caller can invoke Subscription#unsubscribe()
  7. 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
  8. 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(...)
  9. Ex. Double Clicks • Stream of clicks • Accumulate clicks

    in a list • Get length of list • Filter
  10. GitHub Example • Interact with GitHub’s API using Retrofit and

    RxJava ◦ https://github.com/arriolac/GithubRxJava