5. +
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
8. +
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
9. +
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
10. +
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
12. +
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
14. +
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
15. +
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
16. +
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.
17. +
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
20. +
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)
21. +
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
22. +
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.
23. +
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
25. +
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.mainThr
ead())
.subscribeOn(Schedulers.io());
30. +
doOn Operators
■ Note: if only passing on onNext action (without a doOnError) to
the subscribe method OnErrorNotImplementedException will
be thrown if an error occurs
38. +
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)
39. +
map vs. flatMap
map flatMap
When you transform to
a value
When you transform to
an observable
48. +
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