Top Banner
Intro to RxJava on Android Chris Arriola Functional Reactive Programming
27

Introduction to RxJava on Android

Jan 18, 2017

Download

Engineering

Chris Arriola
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Introduction to RxJava on Android

Intro to RxJava on Android

Chris Arriola

Functional Reactive Programming

Page 2: Introduction to RxJava on Android

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?

Page 3: Introduction to RxJava on Android

Nested Network Call w/o RxJava

Page 4: Introduction to RxJava on Android

Nested Network Call w/ RxJava

Page 5: Introduction to RxJava on Android

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

Page 6: Introduction to RxJava on Android

Core RxJava Constructs● Observable

● Observer

● Operator

Page 7: Introduction to RxJava on Android

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

Page 8: Introduction to RxJava on Android

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

Page 9: Introduction to RxJava on Android

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

Page 10: Introduction to RxJava on Android

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

Page 11: Introduction to RxJava on Android

Creating an Observable (cont’d)

● Defer creation of Observable until subscription

Page 12: Introduction to RxJava on Android

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

Page 13: Introduction to RxJava on Android

Unsubscribing to an Observable

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

Page 14: Introduction to RxJava on Android

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

Page 15: Introduction to RxJava on Android

Operator

Page 16: Introduction to RxJava on Android

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

operator

Page 17: Introduction to RxJava on Android

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

Page 18: Introduction to RxJava on Android

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

Page 19: Introduction to RxJava on Android

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

Page 20: Introduction to RxJava on Android

Ex. Double Clicks

Stream of clicks

Accumulate clicks in a list

Get length of list

Filter

Page 21: Introduction to RxJava on Android

Ex. Double ClicksSimulate a stream of clicks:

Page 22: Introduction to RxJava on Android

Ex. Double ClicksAccumulate clicks until 250 ms has passed between clicks:

Page 23: Introduction to RxJava on Android

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

Page 24: Introduction to RxJava on Android
Page 25: Introduction to RxJava on Android

GitHub Example● Interact with GitHub’s API using Retrofit and RxJava

○ https://github.com/arriolac/GithubRxJava

Page 26: Introduction to RxJava on Android

Questions??

Page 27: Introduction to RxJava on Android

Thank You!

Twitter: @arriolachris