View
5.174
Download
0
Category
Tags:
Preview:
Citation preview
+
Reactive
Programming
With RxJava
Mike Nakhimovich
Mnakhimovich@gmail.com
Feb. 23, 2015
+Tonight’s Agenda
■What is Reactive
■Learn By Example
■Walkthrough sample app
+What is
Reactive?
+Reactive Programming
■“Reactive Programming is
programming with
asynchronous data streams.”
(Andre Staltz)
■Data Stream: a sequence of
values
+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
+Functional Reactive
Programming
■Takes reactive
programming to the next
level
■Applying functions to the
data stream
■Ex. Map, Filter, Zip, Take
etc.
+Reactive
Extensions
+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
+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
+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
+From Iterable
To Observable
+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
+Iterable vs. Observable
Interface
■Observable is the
asynchronous/push dual to the
synchronous pull iterable
+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
+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
+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.
+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
+
The Observer
+Observer Interface
■ OnNext
■ onError
■ onCompleted
+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)
+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
+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.
+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
+
Threading Shouldn’t be Hard
+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());
+Available Schedulers
+
Let’s see some example
+Consuming Observables
+Consuming Observables
+doOn Operators
■ Note: if only passing on onNext action (without a doOnError) to the subscribe method OnErrorNotImplementedException will be thrown if an error occurs
+Explicitly Creating Observables
■ You can make an endless Observable by never
calling subscriber.onCompleted()
+From async to sync
Lifesaver during testing
synchronously test asynchronous data
streams
+Need a List?
■Be careful with long/infinite streams
+Combining Observables
+
Map and Flatmap
+Map
+Map
+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)
+map vs. flatMap
map flatMap
When you transform to
a value
When you transform to
an observable
+
Filtering Observables
+Filter
+Filter
+Debounce/Throttle
+Debounce/Throttle
▪ No more sending multiple requests when a
user clicks too many times.
+
Error Recovery
+Recovering from Errors
■ Return a different value in an error case
+
Sample app
walkthrough
https://github.com/Betterment/DaggerStart
er
+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
+Next to Explore
■Cold vs Hot Observables■Subjects■Creating Custom Operators
+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
+
Questions?
Recommended