SlideShare a Scribd company logo
1 of 51
+
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
+ 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.mainThr
ead())
.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?

More Related Content

What's hot

Till Rohrmann – Fault Tolerance and Job Recovery in Apache Flink
Till Rohrmann – Fault Tolerance and Job Recovery in Apache FlinkTill Rohrmann – Fault Tolerance and Job Recovery in Apache Flink
Till Rohrmann – Fault Tolerance and Job Recovery in Apache Flink
Flink Forward
 

What's hot (20)

Javantura v3 - Going Reactive with RxJava – Hrvoje Crnjak
Javantura v3 - Going Reactive with RxJava – Hrvoje CrnjakJavantura v3 - Going Reactive with RxJava – Hrvoje Crnjak
Javantura v3 - Going Reactive with RxJava – Hrvoje Crnjak
 
Reactive Programming and RxJS
Reactive Programming and RxJSReactive Programming and RxJS
Reactive Programming and RxJS
 
Reactive Programming in Java 8 with Rx-Java
Reactive Programming in Java 8 with Rx-JavaReactive Programming in Java 8 with Rx-Java
Reactive Programming in Java 8 with Rx-Java
 
My Gentle Introduction to RxJS
My Gentle Introduction to RxJSMy Gentle Introduction to RxJS
My Gentle Introduction to RxJS
 
Rxjava 介紹與 Android 中的 RxJava
Rxjava 介紹與 Android 中的 RxJavaRxjava 介紹與 Android 中的 RxJava
Rxjava 介紹與 Android 中的 RxJava
 
Journey into Reactive Streams and Akka Streams
Journey into Reactive Streams and Akka StreamsJourney into Reactive Streams and Akka Streams
Journey into Reactive Streams and Akka Streams
 
Reactive Streams: Handling Data-Flow the Reactive Way
Reactive Streams: Handling Data-Flow the Reactive WayReactive Streams: Handling Data-Flow the Reactive Way
Reactive Streams: Handling Data-Flow the Reactive Way
 
RxJava 2.0 介紹
RxJava 2.0 介紹RxJava 2.0 介紹
RxJava 2.0 介紹
 
Reactive Thinking in Java
Reactive Thinking in JavaReactive Thinking in Java
Reactive Thinking in Java
 
RxJava@Android
RxJava@AndroidRxJava@Android
RxJava@Android
 
How to Think in RxJava Before Reacting
How to Think in RxJava Before ReactingHow to Think in RxJava Before Reacting
How to Think in RxJava Before Reacting
 
Reactive Programming with Rx
 Reactive Programming with Rx Reactive Programming with Rx
Reactive Programming with Rx
 
RxJS and Reactive Programming - Modern Web UI - May 2015
RxJS and Reactive Programming - Modern Web UI - May 2015RxJS and Reactive Programming - Modern Web UI - May 2015
RxJS and Reactive Programming - Modern Web UI - May 2015
 
Async and Await on the Server
Async and Await on the ServerAsync and Await on the Server
Async and Await on the Server
 
Till Rohrmann – Fault Tolerance and Job Recovery in Apache Flink
Till Rohrmann – Fault Tolerance and Job Recovery in Apache FlinkTill Rohrmann – Fault Tolerance and Job Recovery in Apache Flink
Till Rohrmann – Fault Tolerance and Job Recovery in Apache Flink
 
The Mayans Lost Guide to RxJava on Android
The Mayans Lost Guide to RxJava on AndroidThe Mayans Lost Guide to RxJava on Android
The Mayans Lost Guide to RxJava on Android
 
Sync with async
Sync with  asyncSync with  async
Sync with async
 
RxJS In-Depth - AngularConnect 2015
RxJS In-Depth - AngularConnect 2015RxJS In-Depth - AngularConnect 2015
RxJS In-Depth - AngularConnect 2015
 
RxJS - The Reactive Extensions for JavaScript
RxJS - The Reactive Extensions for JavaScriptRxJS - The Reactive Extensions for JavaScript
RxJS - The Reactive Extensions for JavaScript
 
Flink Forward SF 2017: Feng Wang & Zhijiang Wang - Runtime Improvements in Bl...
Flink Forward SF 2017: Feng Wang & Zhijiang Wang - Runtime Improvements in Bl...Flink Forward SF 2017: Feng Wang & Zhijiang Wang - Runtime Improvements in Bl...
Flink Forward SF 2017: Feng Wang & Zhijiang Wang - Runtime Improvements in Bl...
 

Similar to Intro to Functional Programming with RxJava

Distributed app development with nodejs and zeromq
Distributed app development with nodejs and zeromqDistributed app development with nodejs and zeromq
Distributed app development with nodejs and zeromq
Ruben Tan
 

Similar to Intro to Functional Programming with RxJava (20)

Introduction to Reactive programming
Introduction to Reactive programmingIntroduction to Reactive programming
Introduction to Reactive programming
 
Reactive programming with RxAndroid
Reactive programming with RxAndroidReactive programming with RxAndroid
Reactive programming with RxAndroid
 
Rxandroid
RxandroidRxandroid
Rxandroid
 
RxAndroid
RxAndroidRxAndroid
RxAndroid
 
RxJava 2 Reactive extensions for the JVM
RxJava 2  Reactive extensions for the JVMRxJava 2  Reactive extensions for the JVM
RxJava 2 Reactive extensions for the JVM
 
RxJava2 Slides
RxJava2 SlidesRxJava2 Slides
RxJava2 Slides
 
Reactive java - Reactive Programming + RxJava
Reactive java - Reactive Programming + RxJavaReactive java - Reactive Programming + RxJava
Reactive java - Reactive Programming + RxJava
 
Algorithms - a brief introduction
Algorithms - a brief introductionAlgorithms - a brief introduction
Algorithms - a brief introduction
 
Rx presentation
Rx presentationRx presentation
Rx presentation
 
Distributed app development with nodejs and zeromq
Distributed app development with nodejs and zeromqDistributed app development with nodejs and zeromq
Distributed app development with nodejs and zeromq
 
Mini training - Reactive Extensions (Rx)
Mini training - Reactive Extensions (Rx)Mini training - Reactive Extensions (Rx)
Mini training - Reactive Extensions (Rx)
 
Reactive programming with Pivotal's reactor
Reactive programming with Pivotal's reactorReactive programming with Pivotal's reactor
Reactive programming with Pivotal's reactor
 
TDC2016SP - Trilha Node.Js
TDC2016SP - Trilha Node.JsTDC2016SP - Trilha Node.Js
TDC2016SP - Trilha Node.Js
 
Rx java workshop
Rx java workshop Rx java workshop
Rx java workshop
 
Streamlining with rx
Streamlining with rxStreamlining with rx
Streamlining with rx
 
Classic synchronization
Classic synchronizationClassic synchronization
Classic synchronization
 
Introduction to reactive programming
Introduction to reactive programmingIntroduction to reactive programming
Introduction to reactive programming
 
TensorFlow Technology
TensorFlow TechnologyTensorFlow Technology
TensorFlow Technology
 
Task parallel library presentation
Task parallel library presentationTask parallel library presentation
Task parallel library presentation
 
Guide to Spring Reactive Programming using WebFlux
Guide to Spring Reactive Programming using WebFluxGuide to Spring Reactive Programming using WebFlux
Guide to Spring Reactive Programming using WebFlux
 

More from Mike Nakhimovich

More from Mike Nakhimovich (7)

meetstore5.pdf
meetstore5.pdfmeetstore5.pdf
meetstore5.pdf
 
Dispatching Reactive State
Dispatching Reactive StateDispatching Reactive State
Dispatching Reactive State
 
Intro to GraphQL on Android with Apollo DroidconNYC 2017
Intro to GraphQL on Android with Apollo DroidconNYC 2017Intro to GraphQL on Android with Apollo DroidconNYC 2017
Intro to GraphQL on Android with Apollo DroidconNYC 2017
 
Data Loading Made Easy with Mike Nakhimovich DroidCon Italy 2017
Data Loading Made Easy with Mike Nakhimovich DroidCon Italy 2017Data Loading Made Easy with Mike Nakhimovich DroidCon Italy 2017
Data Loading Made Easy with Mike Nakhimovich DroidCon Italy 2017
 
Open sourcing the store
Open sourcing the storeOpen sourcing the store
Open sourcing the store
 
Advanced Dagger talk from 360andev
Advanced Dagger talk from 360andevAdvanced Dagger talk from 360andev
Advanced Dagger talk from 360andev
 
Sword fighting with Dagger GDG-NYC Jan 2016
 Sword fighting with Dagger GDG-NYC Jan 2016 Sword fighting with Dagger GDG-NYC Jan 2016
Sword fighting with Dagger GDG-NYC Jan 2016
 

Recently uploaded

The title is not connected to what is inside
The title is not connected to what is insideThe title is not connected to what is inside
The title is not connected to what is inside
shinachiaurasa2
 
%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...
%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...
%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...
masabamasaba
 
%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...
%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...
%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...
masabamasaba
 
%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...
%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...
%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...
masabamasaba
 

Recently uploaded (20)

Crypto Cloud Review - How To Earn Up To $500 Per DAY Of Bitcoin 100% On AutoP...
Crypto Cloud Review - How To Earn Up To $500 Per DAY Of Bitcoin 100% On AutoP...Crypto Cloud Review - How To Earn Up To $500 Per DAY Of Bitcoin 100% On AutoP...
Crypto Cloud Review - How To Earn Up To $500 Per DAY Of Bitcoin 100% On AutoP...
 
%in tembisa+277-882-255-28 abortion pills for sale in tembisa
%in tembisa+277-882-255-28 abortion pills for sale in tembisa%in tembisa+277-882-255-28 abortion pills for sale in tembisa
%in tembisa+277-882-255-28 abortion pills for sale in tembisa
 
%+27788225528 love spells in Vancouver Psychic Readings, Attraction spells,Br...
%+27788225528 love spells in Vancouver Psychic Readings, Attraction spells,Br...%+27788225528 love spells in Vancouver Psychic Readings, Attraction spells,Br...
%+27788225528 love spells in Vancouver Psychic Readings, Attraction spells,Br...
 
The title is not connected to what is inside
The title is not connected to what is insideThe title is not connected to what is inside
The title is not connected to what is inside
 
Direct Style Effect Systems - The Print[A] Example - A Comprehension Aid
Direct Style Effect Systems -The Print[A] Example- A Comprehension AidDirect Style Effect Systems -The Print[A] Example- A Comprehension Aid
Direct Style Effect Systems - The Print[A] Example - A Comprehension Aid
 
%in tembisa+277-882-255-28 abortion pills for sale in tembisa
%in tembisa+277-882-255-28 abortion pills for sale in tembisa%in tembisa+277-882-255-28 abortion pills for sale in tembisa
%in tembisa+277-882-255-28 abortion pills for sale in tembisa
 
%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...
%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...
%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...
 
The Top App Development Trends Shaping the Industry in 2024-25 .pdf
The Top App Development Trends Shaping the Industry in 2024-25 .pdfThe Top App Development Trends Shaping the Industry in 2024-25 .pdf
The Top App Development Trends Shaping the Industry in 2024-25 .pdf
 
%in ivory park+277-882-255-28 abortion pills for sale in ivory park
%in ivory park+277-882-255-28 abortion pills for sale in ivory park %in ivory park+277-882-255-28 abortion pills for sale in ivory park
%in ivory park+277-882-255-28 abortion pills for sale in ivory park
 
%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...
%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...
%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...
 
%in kaalfontein+277-882-255-28 abortion pills for sale in kaalfontein
%in kaalfontein+277-882-255-28 abortion pills for sale in kaalfontein%in kaalfontein+277-882-255-28 abortion pills for sale in kaalfontein
%in kaalfontein+277-882-255-28 abortion pills for sale in kaalfontein
 
VTU technical seminar 8Th Sem on Scikit-learn
VTU technical seminar 8Th Sem on Scikit-learnVTU technical seminar 8Th Sem on Scikit-learn
VTU technical seminar 8Th Sem on Scikit-learn
 
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
 
%in Hazyview+277-882-255-28 abortion pills for sale in Hazyview
%in Hazyview+277-882-255-28 abortion pills for sale in Hazyview%in Hazyview+277-882-255-28 abortion pills for sale in Hazyview
%in Hazyview+277-882-255-28 abortion pills for sale in Hazyview
 
Announcing Codolex 2.0 from GDK Software
Announcing Codolex 2.0 from GDK SoftwareAnnouncing Codolex 2.0 from GDK Software
Announcing Codolex 2.0 from GDK Software
 
%in Midrand+277-882-255-28 abortion pills for sale in midrand
%in Midrand+277-882-255-28 abortion pills for sale in midrand%in Midrand+277-882-255-28 abortion pills for sale in midrand
%in Midrand+277-882-255-28 abortion pills for sale in midrand
 
%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...
%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...
%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...
 
Define the academic and professional writing..pdf
Define the academic and professional writing..pdfDefine the academic and professional writing..pdf
Define the academic and professional writing..pdf
 
%in Durban+277-882-255-28 abortion pills for sale in Durban
%in Durban+277-882-255-28 abortion pills for sale in Durban%in Durban+277-882-255-28 abortion pills for sale in Durban
%in Durban+277-882-255-28 abortion pills for sale in Durban
 
OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...
OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...
OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...
 

Intro to Functional Programming with RxJava