$30 off During Our Annual Pro Sale. View Details »

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. Intro to RxJava on Android
    Chris Arriola
    Functional Reactive Programming

    View Slide

  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?

    View Slide

  3. Nested Network Call w/o RxJava

    View Slide

  4. Nested Network Call w/ RxJava

    View Slide

  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

    View Slide

  6. Core RxJava Constructs
    ● Observable
    ● Observer
    ● Operator

    View Slide

  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

    View Slide

  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()

    View Slide

  9. Creating an Observable
    ● Create an Observable using #create(...)

    View Slide

  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(...)

    View Slide

  11. Creating an Observable (cont’d)
    ● Defer creation of Observable until subscription

    View Slide

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

    View Slide

  13. Unsubscribing to an Observable
    ● Observable#subscribe(...) returns a Subscription object from which the
    caller can invoke Subscription#unsubscribe()

    View Slide

  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

    View Slide

  15. Operator

    View Slide

  16. Transforming an Observable
    ● Transform values emitted by Observable using the #map(...) operator

    View Slide

  17. Filtering an Observable
    ● Filter values emitted by Observable using #filter(...) operator

    View Slide

  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(...)

    View Slide

  19. Error Handling
    ● Re-subscribe/retry when the Observable emits an error

    View Slide

  20. Ex. Double Clicks
    ● Stream of clicks
    ● Accumulate clicks in a list
    ● Get length of list
    ● Filter

    View Slide

  21. Ex. Double Clicks
    Simulate a stream of clicks:

    View Slide

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

    View Slide

  23. Ex. Double Clicks
    Map the length of each list & filter for sizes >= 2:

    View Slide

  24. View Slide

  25. GitHub Example
    ● Interact with GitHub’s API using Retrofit and RxJava
    ○ https://github.com/arriolac/GithubRxJava

    View Slide

  26. Questions??

    View Slide

  27. Thank You!
    Twitter: @arriolachris

    View Slide