Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.
+
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:...
+
What does this mean?
■Programming Model based on the
principle of push rather than pull
■Values are emitted when ready,
...
+
Functional Reactive
Programming
■Takes reactive
programming to the next
level
■Applying functions to the
data stream
■Ex...
+ Reactive
Extensions
+
ReactiveX
■Collection of helpful functions that
let you do reactive programming
■ReactiveX exists in more than 10
langua...
+
Building Blocks of RxJava
■ Observable: source of data stream (sender)
■ Observer: listens for emitted values
(receiver)...
+
Observable Observer Pattern
■Allows for Concurrent Operations:
the observer does not need to
block while waiting for the...
+ From Iterable
To Observable
+
Before Observables
■No easy way to perform asynchronous
operations if you needed multiple items
■Observables fill the ga...
+
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 (ne...
+
Iterable Architecture
■ Before Reactive
1. Call a method
2. Wait for result
3. Store the return value from that method i...
+
Observable Architecture
■ The flow goes like this:
1. Define an Observer that specifies what to
do with each emitted val...
+
In RxJava…
■ The Subscribe method connects an
Observer to an Observable
■ Once you Subscribe, no need to block the
threa...
+
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
t...
+
onError
■ Observable calls this method to indicate
that it has failed to generate the expected
data or has encountered s...
+
onComplete
■ Observable calls this method after it has
called onNext for the final time and it has not
encountered any e...
+
Why RxJava?
■Schedulers that make threading a
breeze
■Operators that let you transform,
combine, manipulate, and work
wi...
+
Threading Shouldn’t be Hard
+
Schedulers
■ Schedulers are used to manage and control
concurrency
■ observeOn: thread observable is executed on
■ subsc...
+
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 OnErrorNotImplemen...
+
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 Observabl...
+
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 d...
+
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...
+
Questions?
Upcoming SlideShare
Loading in …5
×

Intro to Functional Programming with RxJava

20,973 views

Published on

Slides from NYCJava talk on Intro To Functional Programming with RxJava.
Sample project:
https://github.com/digitalbuddha/RxStarter

Published in: Software
  • Hello! Get Your Professional Job-Winning Resume Here - Check our website! https://vk.cc/818RFv
       Reply 
    Are you sure you want to  Yes  No
    Your message goes here

Intro to Functional Programming with RxJava

  1. 1. + Reactive Programming With RxJava Mike Nakhimovich Mnakhimovich@gmail.com Feb. 23, 2015
  2. 2. + Tonight’s Agenda ■What is Reactive ■Learn By Example ■Walkthrough sample app
  3. 3. + What is Reactive?
  4. 4. + Reactive Programming ■“Reactive Programming is programming with asynchronous data streams.” (Andre Staltz) ■Data Stream: a sequence of values
  5. 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
  6. 6. + Functional Reactive Programming ■Takes reactive programming to the next level ■Applying functions to the data stream ■Ex. Map, Filter, Zip, Take
  7. 7. + Reactive Extensions
  8. 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. 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. 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
  11. 11. + From Iterable To Observable
  12. 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
  13. 13. + Iterable vs. Observable Interface ■Observable is the asynchronous/push dual to the synchronous pull iterable
  14. 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. 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. 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. 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
  18. 18. + The Observer
  19. 19. + Observer Interface ■ OnNext ■ onError ■ onCompleted
  20. 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. 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. 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. 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
  24. 24. + Threading Shouldn’t be Hard
  25. 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());
  26. 26. + Available Schedulers
  27. 27. + Let’s see some example
  28. 28. + Consuming Observables
  29. 29. + Consuming Observables
  30. 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
  31. 31. + Explicitly Creating Observables ■ You can make an endless Observable by never calling subscriber.onCompleted()
  32. 32. + From async to sync Lifesaver during testing synchronously test asynchronous data streams
  33. 33. + Need a List? ■Be careful with long/infinite streams
  34. 34. + Combining Observables
  35. 35. + Map and Flatmap
  36. 36. + Map
  37. 37. + Map
  38. 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. 39. + map vs. flatMap map flatMap When you transform to a value When you transform to an observable
  40. 40. + Filtering Observables
  41. 41. + Filter
  42. 42. + Filter
  43. 43. + Debounce/Throttle
  44. 44. + Debounce/Throttle ▪ No more sending multiple requests when a user clicks too many times.
  45. 45. + Error Recovery
  46. 46. + Recovering from Errors ■ Return a different value in an error case
  47. 47. + Sample app walkthrough https://github.com/Betterment/DaggerStart er
  48. 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
  49. 49. + Next to Explore ■Cold vs Hot Observables ■Subjects ■Creating Custom Operators
  50. 50. + 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
  51. 51. + Questions?

×