Intro to Functional Programming with RxJava

51
+ Reactive Programming With RxJava Mike Nakhimovich [email protected] Feb. 23, 2015

Transcript of Intro to Functional Programming with RxJava

Page 1: Intro to Functional Programming with RxJava

+

Reactive

Programming

With RxJava

Mike Nakhimovich

[email protected]

Feb. 23, 2015

Page 2: Intro to Functional Programming with RxJava

+Tonight’s Agenda

■What is Reactive

■Learn By Example

■Walkthrough sample app

Page 3: Intro to Functional Programming with RxJava

+What is

Reactive?

Page 4: Intro to Functional Programming with RxJava

+Reactive Programming

■“Reactive Programming is

programming with

asynchronous data streams.”

(Andre Staltz)

■Data Stream: a sequence of

values

Page 5: Intro to Functional Programming with RxJava

+What does this mean?

■Programming Model based on the

principle of push rather than pull

■Values are emitted when ready,

not when asked for in a non-

blocking manner

■Allows for actions to be performed

in parallel, rather than in serial

■ ie: shopping in person vs online

Page 6: Intro to Functional Programming with RxJava

+Functional Reactive

Programming

■Takes reactive

programming to the next

level

■Applying functions to the

data stream

■Ex. Map, Filter, Zip, Take

etc.

Page 7: Intro to Functional Programming with RxJava

+Reactive

Extensions

Page 8: Intro to Functional Programming with RxJava

+ReactiveX

■Collection of helpful functions that

let you do reactive programming

■ReactiveX exists in more than 10

languages (JavaScript, .Net,

Objective-C, etc.)

■RxJava is the Java

implementation of ReactiveX

■Ported by Netflix team

Page 9: Intro to Functional Programming with RxJava

+Building Blocks of RxJava

■ Observable: source of data stream (sender)

■ Observer: listens for emitted values (receiver)

■ The Observer subscribes (listens) to the Observable

■ Observers react to whatever item or sequence of items the Observable emits

■ Many observers can subscribe to the same observable

Page 10: Intro to Functional Programming with RxJava

+Observable Observer Pattern

■Allows for Concurrent Operations:

the observer does not need to

block while waiting for the

observable to emit values

■Observer waits to receive values

when the observable is ready to

emit them

■Based on push rather than pull

Page 11: Intro to Functional Programming with RxJava

+From Iterable

To Observable

Page 12: Intro to Functional Programming with RxJava

+Before Observables

■No easy way to perform asynchronous operations if you needed multiple items

■Observables fill the gap as the ideal way to access asynchronous sequences of multiple items

Page 13: Intro to Functional Programming with RxJava

+Iterable vs. Observable

Interface

■Observable is the

asynchronous/push dual to the

synchronous pull iterable

Page 14: Intro to Functional Programming with RxJava

+Observables are…

■ Composable: Easily chained together or

combined

■ Flexible: Can be used to emit:

■ A scalar value (network result)

■ Sequence (items in a list)

■ Infinite streams (weather sensor)

■ Free from callback hell: Easy to transform

one asynchronous stream into another

Page 15: Intro to Functional Programming with RxJava

+Iterable Architecture

■ Before Reactive

1. Call a method

2. Wait for result

3. Store the return value from that method in a variable

4. Use that variable and its new value to do something useful

Page 16: Intro to Functional Programming with RxJava

+Observable Architecture

■ The flow goes like this:

1. Define an Observer that specifies what to

do with each emitted value

2. Call a method that returns an Observable

3. Subscribe the Observer to the Observable.

This tells the Observable that it has a

subscriber waiting to receive values when

they’re available.

Page 17: Intro to Functional Programming with RxJava

+In RxJava…

■ The Subscribe method connects an

Observer to an Observable

■ Once you Subscribe, no need to block the

thread

■ Values will come to your Observer when they

are ready

Page 18: Intro to Functional Programming with RxJava

+

The Observer

Page 19: Intro to Functional Programming with RxJava

+Observer Interface

■ OnNext

■ onError

■ onCompleted

Page 20: Intro to Functional Programming with RxJava

+onNext

■ Observable calls this method whenever the

Observable emits an item.

■ This method can be called any number of

times (zero to many)

■ Always followed by onError or onComplete

(but not both)

Page 21: Intro to Functional Programming with RxJava

+onError

■ Observable calls this method to indicate

that it has failed to generate the expected

data or has encountered some other error

■ This stops the Observable and it won’t

make further calls.

■ Takes as its parameter an indication of

what caused the error

Page 22: Intro to Functional Programming with RxJava

+onComplete

■ Observable calls this method after it has

called onNext for the final time and it has not

encountered any errors.

■ A call to onComplete ends the subscription.

Page 23: Intro to Functional Programming with RxJava

+Why RxJava?

■Schedulers that make threading a

breeze

■Operators that let you transform,

combine, manipulate, and work

with the sequence of items emitted

by Observables

Page 24: Intro to Functional Programming with RxJava

+

Threading Shouldn’t be Hard

Page 25: Intro to Functional Programming with RxJava

+Schedulers

■ Schedulers are used to manage and control concurrency

■ observeOn: thread observable is executed on

■ subscribeOn: thread subscribe is executed on

api.users()

.observeOn(AndroidSchedulers.mainThread())

.subscribeOn(Schedulers.io());

Page 26: Intro to Functional Programming with RxJava

+Available Schedulers

Page 27: Intro to Functional Programming with RxJava

+

Let’s see some example

Page 28: Intro to Functional Programming with RxJava

+Consuming Observables

Page 29: Intro to Functional Programming with RxJava

+Consuming Observables

Page 30: Intro to Functional Programming with RxJava

+doOn Operators

■ Note: if only passing on onNext action (without a doOnError) to the subscribe method OnErrorNotImplementedException will be thrown if an error occurs

Page 31: Intro to Functional Programming with RxJava

+Explicitly Creating Observables

■ You can make an endless Observable by never

calling subscriber.onCompleted()

Page 32: Intro to Functional Programming with RxJava

+From async to sync

Lifesaver during testing

synchronously test asynchronous data

streams

Page 33: Intro to Functional Programming with RxJava

+Need a List?

■Be careful with long/infinite streams

Page 34: Intro to Functional Programming with RxJava

+Combining Observables

Page 35: Intro to Functional Programming with RxJava

+

Map and Flatmap

Page 36: Intro to Functional Programming with RxJava

+Map

Page 37: Intro to Functional Programming with RxJava

+Map

Page 38: Intro to Functional Programming with RxJava

+Transform One Observable into

Another

1. Create on observable from a click event

2. Use flatMap to change that Observable to

another Observable

3. Subscribe to the result of the second

Observable (will emit 3 users)

Page 39: Intro to Functional Programming with RxJava

+map vs. flatMap

map flatMap

When you transform to

a value

When you transform to

an observable

Page 40: Intro to Functional Programming with RxJava

+

Filtering Observables

Page 41: Intro to Functional Programming with RxJava

+Filter

Page 42: Intro to Functional Programming with RxJava

+Filter

Page 43: Intro to Functional Programming with RxJava

+Debounce/Throttle

Page 44: Intro to Functional Programming with RxJava

+Debounce/Throttle

▪ No more sending multiple requests when a

user clicks too many times.

Page 45: Intro to Functional Programming with RxJava

+

Error Recovery

Page 46: Intro to Functional Programming with RxJava

+Recovering from Errors

■ Return a different value in an error case

Page 47: Intro to Functional Programming with RxJava

+

Sample app

walkthrough

https://github.com/Betterment/DaggerStart

er

Page 48: Intro to Functional Programming with RxJava

+How we use Rx

■ Treat all data as immutable singleton Observable Stores that know how to self-update and are backed on disk

■ Since we treat data as a stream, we can return 1 or more results for each subscription (if a cached value is available)

■ We can cross subscribe one Store to another

■ RxJava lets us transform and combine data on any thread and subscribe to updates for related values/collections

Page 49: Intro to Functional Programming with RxJava

+Next to Explore

■Cold vs Hot Observables■Subjects■Creating Custom Operators

Page 50: Intro to Functional Programming with RxJava

+Sources/Addl Reading

■Offical Wiki https://github.com/ReactiveX/RxJava/wiki■Mastering Observables http://docs.couchbase.com/developer/java-2.0/observables.html■Intro To Functional Reactive Programming https://gist.github.com/staltz/868e7e9bc2a7b8c1f754

Page 51: Intro to Functional Programming with RxJava

+

Questions?