SlideShare a Scribd company logo
+
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 (20)

PPTX
Javantura v3 - Going Reactive with RxJava – Hrvoje Crnjak
HUJAK - Hrvatska udruga Java korisnika / Croatian Java User Association
 
PPTX
Reactive Programming and RxJS
Denis Gorbunov
 
PPTX
Reactive Programming in Java 8 with Rx-Java
Kasun Indrasiri
 
PDF
My Gentle Introduction to RxJS
Mattia Occhiuto
 
PDF
Rxjava 介紹與 Android 中的 RxJava
Kros Huang
 
PDF
Journey into Reactive Streams and Akka Streams
Kevin Webber
 
PDF
Reactive Streams: Handling Data-Flow the Reactive Way
Roland Kuhn
 
PDF
RxJava 2.0 介紹
Kros Huang
 
PDF
Reactive Thinking in Java
Yakov Fain
 
PDF
RxJava@Android
Maxim Volgin
 
PDF
How to Think in RxJava Before Reacting
IndicThreads
 
PDF
Reactive Programming with Rx
C4Media
 
PPTX
RxJS and Reactive Programming - Modern Web UI - May 2015
Ben Lesh
 
PPTX
Async and Await on the Server
Doug Jones
 
PPTX
Till Rohrmann – Fault Tolerance and Job Recovery in Apache Flink
Flink Forward
 
PDF
The Mayans Lost Guide to RxJava on Android
Fernando Cejas
 
PPTX
Sync with async
prabathsl
 
PPTX
RxJS In-Depth - AngularConnect 2015
Ben Lesh
 
PDF
RxJS - The Reactive Extensions for JavaScript
Viliam Elischer
 
PDF
Flink Forward SF 2017: Feng Wang & Zhijiang Wang - Runtime Improvements in Bl...
Flink Forward
 
Javantura v3 - Going Reactive with RxJava – Hrvoje Crnjak
HUJAK - Hrvatska udruga Java korisnika / Croatian Java User Association
 
Reactive Programming and RxJS
Denis Gorbunov
 
Reactive Programming in Java 8 with Rx-Java
Kasun Indrasiri
 
My Gentle Introduction to RxJS
Mattia Occhiuto
 
Rxjava 介紹與 Android 中的 RxJava
Kros Huang
 
Journey into Reactive Streams and Akka Streams
Kevin Webber
 
Reactive Streams: Handling Data-Flow the Reactive Way
Roland Kuhn
 
RxJava 2.0 介紹
Kros Huang
 
Reactive Thinking in Java
Yakov Fain
 
RxJava@Android
Maxim Volgin
 
How to Think in RxJava Before Reacting
IndicThreads
 
Reactive Programming with Rx
C4Media
 
RxJS and Reactive Programming - Modern Web UI - May 2015
Ben Lesh
 
Async and Await on the Server
Doug Jones
 
Till Rohrmann – Fault Tolerance and Job Recovery in Apache Flink
Flink Forward
 
The Mayans Lost Guide to RxJava on Android
Fernando Cejas
 
Sync with async
prabathsl
 
RxJS In-Depth - AngularConnect 2015
Ben Lesh
 
RxJS - The Reactive Extensions for JavaScript
Viliam Elischer
 
Flink Forward SF 2017: Feng Wang & Zhijiang Wang - Runtime Improvements in Bl...
Flink Forward
 

Similar to Intro to Functional Programming with RxJava (20)

PPTX
Introduction to Reactive programming
Dwi Randy Herdinanto
 
PPTX
Reactive programming with RxAndroid
Savvycom Savvycom
 
PPTX
Rxandroid
Thinh Thanh
 
PPTX
RxAndroid
Thinh Thanh
 
PPTX
RxJava 2 Reactive extensions for the JVM
Netesh Kumar
 
PPTX
RxJava2 Slides
YarikS
 
PDF
Reactive java - Reactive Programming + RxJava
NexThoughts Technologies
 
PDF
Algorithms - a brief introduction
Giacomo Belocchi
 
PPTX
Rx presentation
Ali Mahfud
 
KEY
Distributed app development with nodejs and zeromq
Ruben Tan
 
PPTX
Mini training - Reactive Extensions (Rx)
Betclic Everest Group Tech Team
 
PDF
Reactive programming with Pivotal's reactor
VMware Tanzu
 
PPTX
TDC2016SP - Trilha Node.Js
tdc-globalcode
 
PDF
Rx java workshop
Mayowa Egbewunmi
 
PDF
Streamlining with rx
Akhil Dad
 
PPT
Classic synchronization
hina firdaus
 
PDF
Introduction to reactive programming
Leapfrog Technology Inc.
 
PPTX
TensorFlow Technology
narayan dudhe
 
PPTX
Task parallel library presentation
ahmed sayed
 
PDF
Guide to Spring Reactive Programming using WebFlux
Inexture Solutions
 
Introduction to Reactive programming
Dwi Randy Herdinanto
 
Reactive programming with RxAndroid
Savvycom Savvycom
 
Rxandroid
Thinh Thanh
 
RxAndroid
Thinh Thanh
 
RxJava 2 Reactive extensions for the JVM
Netesh Kumar
 
RxJava2 Slides
YarikS
 
Reactive java - Reactive Programming + RxJava
NexThoughts Technologies
 
Algorithms - a brief introduction
Giacomo Belocchi
 
Rx presentation
Ali Mahfud
 
Distributed app development with nodejs and zeromq
Ruben Tan
 
Mini training - Reactive Extensions (Rx)
Betclic Everest Group Tech Team
 
Reactive programming with Pivotal's reactor
VMware Tanzu
 
TDC2016SP - Trilha Node.Js
tdc-globalcode
 
Rx java workshop
Mayowa Egbewunmi
 
Streamlining with rx
Akhil Dad
 
Classic synchronization
hina firdaus
 
Introduction to reactive programming
Leapfrog Technology Inc.
 
TensorFlow Technology
narayan dudhe
 
Task parallel library presentation
ahmed sayed
 
Guide to Spring Reactive Programming using WebFlux
Inexture Solutions
 
Ad

More from Mike Nakhimovich (7)

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

Recently uploaded (20)

PPTX
Comprehensive Guide: Shoviv Exchange to Office 365 Migration Tool 2025
Shoviv Software
 
PDF
Continouous failure - Why do we make our lives hard?
Papp Krisztián
 
PPTX
How Apagen Empowered an EPC Company with Engineering ERP Software
SatishKumar2651
 
PDF
Alarm in Android-Scheduling Timed Tasks Using AlarmManager in Android.pdf
Nabin Dhakal
 
PDF
Understanding the Need for Systemic Change in Open Source Through Intersectio...
Imma Valls Bernaus
 
PDF
Thread In Android-Mastering Concurrency for Responsive Apps.pdf
Nabin Dhakal
 
PDF
Revenue streams of the Wazirx clone script.pdf
aaronjeffray
 
PPTX
How Odoo Became a Game-Changer for an IT Company in Manufacturing ERP
SatishKumar2651
 
PDF
Salesforce CRM Services.VALiNTRY360
VALiNTRY360
 
PDF
GetOnCRM Speeds Up Agentforce 3 Deployment for Enterprise AI Wins.pdf
GetOnCRM Solutions
 
PDF
Capcut Pro Crack For PC Latest Version {Fully Unlocked} 2025
hashhshs786
 
PPTX
Hardware(Central Processing Unit ) CU and ALU
RizwanaKalsoom2
 
PPTX
Fundamentals_of_Microservices_Architecture.pptx
MuhammadUzair504018
 
PPTX
Migrating Millions of Users with Debezium, Apache Kafka, and an Acyclic Synch...
MD Sayem Ahmed
 
PDF
Executive Business Intelligence Dashboards
vandeslie24
 
PDF
Letasoft Sound Booster 1.12.0.538 Crack Download+ Product Key [Latest]
HyperPc soft
 
PPTX
The Role of a PHP Development Company in Modern Web Development
SEO Company for School in Delhi NCR
 
PDF
vMix Pro 28.0.0.42 Download vMix Registration key Bundle
kulindacore
 
PPTX
Feb 2021 Cohesity first pitch presentation.pptx
enginsayin1
 
PDF
MiniTool Partition Wizard 12.8 Crack License Key LATEST
hashhshs786
 
Comprehensive Guide: Shoviv Exchange to Office 365 Migration Tool 2025
Shoviv Software
 
Continouous failure - Why do we make our lives hard?
Papp Krisztián
 
How Apagen Empowered an EPC Company with Engineering ERP Software
SatishKumar2651
 
Alarm in Android-Scheduling Timed Tasks Using AlarmManager in Android.pdf
Nabin Dhakal
 
Understanding the Need for Systemic Change in Open Source Through Intersectio...
Imma Valls Bernaus
 
Thread In Android-Mastering Concurrency for Responsive Apps.pdf
Nabin Dhakal
 
Revenue streams of the Wazirx clone script.pdf
aaronjeffray
 
How Odoo Became a Game-Changer for an IT Company in Manufacturing ERP
SatishKumar2651
 
Salesforce CRM Services.VALiNTRY360
VALiNTRY360
 
GetOnCRM Speeds Up Agentforce 3 Deployment for Enterprise AI Wins.pdf
GetOnCRM Solutions
 
Capcut Pro Crack For PC Latest Version {Fully Unlocked} 2025
hashhshs786
 
Hardware(Central Processing Unit ) CU and ALU
RizwanaKalsoom2
 
Fundamentals_of_Microservices_Architecture.pptx
MuhammadUzair504018
 
Migrating Millions of Users with Debezium, Apache Kafka, and an Acyclic Synch...
MD Sayem Ahmed
 
Executive Business Intelligence Dashboards
vandeslie24
 
Letasoft Sound Booster 1.12.0.538 Crack Download+ Product Key [Latest]
HyperPc soft
 
The Role of a PHP Development Company in Modern Web Development
SEO Company for School in Delhi NCR
 
vMix Pro 28.0.0.42 Download vMix Registration key Bundle
kulindacore
 
Feb 2021 Cohesity first pitch presentation.pptx
enginsayin1
 
MiniTool Partition Wizard 12.8 Crack License Key LATEST
hashhshs786
 

Intro to Functional Programming with RxJava