Slide 1

Slide 1 text

Intro to RxJava on Android Chris Arriola Functional Reactive Programming

Slide 2

Slide 2 text

● 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?

Slide 3

Slide 3 text

Nested Network Call w/o RxJava

Slide 4

Slide 4 text

Nested Network Call w/ RxJava

Slide 5

Slide 5 text

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

Slide 6

Slide 6 text

Core RxJava Constructs ● Observable ● Observer ● Operator

Slide 7

Slide 7 text

● 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

Slide 8

Slide 8 text

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

Slide 9

Slide 9 text

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

Slide 10

Slide 10 text

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

Slide 11

Slide 11 text

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

Slide 12

Slide 12 text

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

Slide 13

Slide 13 text

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

Slide 14

Slide 14 text

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

Slide 15

Slide 15 text

Operator

Slide 16

Slide 16 text

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

Slide 17

Slide 17 text

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

Slide 18

Slide 18 text

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

Slide 19

Slide 19 text

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

Slide 20

Slide 20 text

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

Slide 21

Slide 21 text

Ex. Double Clicks Simulate a stream of clicks:

Slide 22

Slide 22 text

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

Slide 23

Slide 23 text

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

Slide 24

Slide 24 text

No content

Slide 25

Slide 25 text

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

Slide 26

Slide 26 text

Questions??

Slide 27

Slide 27 text

Thank You! Twitter: @arriolachris