SlideShare a Scribd company logo
An Introduction
to RxJava
Matt Dupree
An Introduction to RxJava
An Introduction to RxJava
An Introduction to RxJava
mSearchView.setOnQueryTextListener(

new SearchView.OnQueryTextListener() {

@Override

public boolean onQueryTextSubmit(String s) {

mSearchView.clearFocus();

return true;

}



@Override

public boolean onQueryTextChange(String s) {

searchFor(s);

return true;

}

});
mSearchView.setOnQueryTextListener(

new SearchView.OnQueryTextListener() {

@Override

public boolean onQueryTextSubmit(String s) {

mSearchView.clearFocus();

return true;

}



@Override

public boolean onQueryTextChange(String s) {

searchFor(s);

return true;

}

});
private void searchFor(String query) {

// ANALYTICS EVENT: Start a search on the Search activity

// Contains: Nothing (Event params are constant: Search query 

// not included)

AnalyticsHelper.sendEvent(SCREEN_LABEL, "Search", "");

Bundle args = new Bundle(1);

if (query == null) {

query = "";

}

args.putString(ARG_QUERY, query);

if (TextUtils.equals(query, mQuery)) {

getLoaderManager()

.initLoader(SearchTopicsSessionsQuery.TOKEN, args,

this);

} else {

getLoaderManager()

.restartLoader(SearchTopicsSessionsQuery.TOKEN, args,

this);

}

mQuery = query;

}
private void searchFor(String query) {

// ANALYTICS EVENT: Start a search on the Search activity

// Contains: Nothing (Event params are constant: Search query 

// not included)

AnalyticsHelper.sendEvent(SCREEN_LABEL, "Search", "");

Bundle args = new Bundle(1);

if (query == null) {

query = "";

}

args.putString(ARG_QUERY, query);

if (TextUtils.equals(query, mQuery)) {

getLoaderManager()

.initLoader(SearchTopicsSessionsQuery.TOKEN, args,

this);

} else {

getLoaderManager()

.restartLoader(SearchTopicsSessionsQuery.TOKEN, args,

this);

}

mQuery = query;

}
@Override

public void onLoadFinished(Loader<Cursor> loader, Cursor data) {

mResultsAdapter.swapCursor(data);

mSearchResults.setVisibility(

data.getCount() > 0 ? View.VISIBLE : View.GONE);

}
we’re missing an
abstraction
Observable
declaratively
composable sequence
declaratively
composable sequence
declaratively
composable sequence
Observables in Action
I was pretty much dragged into RxJava by my coworkers...[RxJava]
was a lot like git...when I first learned git, I didn’t really learn it. I just
spent three weeks being mad at it...and then something clicked
and I was like ‘Oh! I get it! And this is amazing and I love it!' The
same thing happened with RxJava.
—Dan Lew, Google Developer Expert
Java’s
Array
Memory
Composition
Rx
Observable
Java’s
Array
Memory
Composition
Rx
Observable
Java’s
Array
Memory
Composition
Rx
Observable
Java’s
Array
Java’s
Iterable
Memory
Composition
Kotlin’s
Iterable;
Java 8
Streams
Rx
Observable
Java’s
Array
Java’s
Iterable
Memory
Composition
Kotlin’s
Iterable;
Java 8
Streams
Rx
Observable
Java’s
Array
Java’s
Iterable
Memory
Composition
Kotlin’s
Iterable;
Java 8
Streams
Rx
Observable
Java’s
Array
Java’s
Iterable
Memory
Composition
Kotlin’s
Iterable;
Java 8
Streams
Rx
Observable
Java’s
Array
Java’s
Iterable
Memory
Composition
Kotlin’s
Iterable;
Java 8
Streams
Rx
Observable
???
???
declaratively
composable sequence
declaratively
composable sequence
Java’s
Array
Java’s
Iterable
Memory
Composition
Kotlin’s
Iterable;
Java 8
Streams
Rx
Observable
Java’s
Array
Java’s
Iterable
Memory
Composition
Kotlin’s
Iterable;
Java 8
Streams
Rx
Observable
Javascript’s
Array
Java’s
Array
Java’s
Iterable
Memory
Composition
Kotlin’s
Iterable;
Java 8
Streams
Rx
Observable
Javascript’s
Array
declaratively
composable array
imperatively
composable array
Java’s
Array
Java’s
Iterable
Memory
Composition
Kotlin’s
Iterable;
Java 8
Streams
Rx
Observable
Javascript’s
Array
Country[] northAmericanCountries = { 

new Country("United States", 300e6),

new Country("Canada", 36e6),

new Country("Mexico", 127e6),

//...

};
double[] getPopulations(Country[] counties) {

double[] populations = new double[counties.length];

for (int i = 0; i < counties.length; i++) {

populations[i] = counties[i].getPopulation();

}

return populations;

}
double[] getPopulations(Country[] counties) {

double[] populations = new double[counties.length];

for (int i = 0; i < counties.length; i++) {

populations[i] = counties[i].getPopulation();

}

return populations;

}
double[] getPopulations(Country[] counties) {

double[] populations = new double[counties.length];

for (int i = 0; i < counties.length; i++) {

populations[i] = counties[i].getPopulation();

}

return populations;

}
double[] getPopulations(Country[] counties) {

double[] populations = new double[counties.length];

for (int i = 0; i < counties.length; i++) {

populations[i] = counties[i].getPopulation();

}

return populations;

}
Java’s
Array
Java’s
Iterable
Memory
Composition
Kotlin’s
Iterable;
Java 8
Streams
Rx
Observable
Javascript’s
Array
Java’s
Array
Java’s
Iterable
Memory
Composition
Kotlin’s
Iterable;
Java 8
Streams
Rx
Observable
Javascript’s
Array
const countries = [
{ name: "United States", population: 300e6 },
{ name: "Canada", population: 36e6 },
{ name: "Mexico", population: 107e6 }
];
const getPopulations
= (countries) => countries.map(it => it.population);
class Array {
map(transform) {
const newList = [];
for (let i = 0; i < this.size; i++) {
newList.push(transform(this[i]));
}
return newList;
}
}
map
map
filter
map
filter
reduce
const northAmericanPopExcludingUs = countries
.filter(it => it.name === "United States")
.map(it => it.population)
.reduce((totalPop, countryPop) => totalPop + countryPop);
const northAmericanPopExcludingUs = countries
.filter(it => it.name === "United States")
.map(it => it.population)
.reduce((totalPop, countryPop) => totalPop + countryPop);
const northAmericanPopExcludingUs = countries
.filter(it => it.name === "United States")
.map(it => it.population)
.reduce((totalPop, countryPop) => totalPop + countryPop);
const northAmericanPopExcludingUs = countries
.filter(it => it.name === "United States")
.map(it => it.population)
.reduce((totalPop, countryPop) => totalPop + countryPop);
const northAmericanPopExcludingUs = countries
.filter(it => it.name === "United States")
.map(it => it.population)
.reduce((totalPop, countryPop) => totalPop + countryPop);
const northAmericanPopExcludingUs = countries
.filter(it => it.name === "United States")
.map(it => it.population)
.reduce((totalPop, countryPop) => totalPop + countryPop);
300e6 + 36e6 + 127e6 + …
declaratively
composable array
Java’s
Array
Java’s
Iterable
Memory
Composition
Kotlin’s
Iterable;
Java 8
Streams
Rx
Observable
Javascript’s
Array
map
filter
reduce
map
filter
reduce
+ LinkedList
map
filter
reduce
+ LinkedList = 💥
How is the sequence
stored in memory?
Java’s
Array
Java’s
Iterable
Memory
Composition
Kotlin’s
Iterable;
Java 8
Streams
Rx
Observable
Javascript’s
Array
Java’s
Array
Java’s
Iterable
Memory
Composition
Kotlin’s
Iterable;
Java 8
Streams
Rx
Observable
Javascript’s
Array
Array
Array LinkedList
Array LinkedList
Iterable
Country[] northAmericanCountries = { 

new Country("United States", 300e6),

new Country("Canada", 36e6),

new Country("Mexico", 127e6),

//...

};
for (int i = 0; i < northAmericanCountries.length; i++) {

System.out.println(northAmericanCountries[i]);

}
for (int i = 0; i < linkedList.length; i++) {

System.out.println(linkedList[i]);

}
for (final Object object : array) {

System.out.println(object);

}
for (final Object object : linkedList) {

System.out.println(object);

}
for (final Object object : iterable) {

System.out.println(object);

}
How is the sequence
stored in memory?
Java’s
Array
Java’s
Iterable
Memory
Composition
Kotlin’s
Iterable;
Java 8
Streams
Rx
Observable
Javascript’s
Array
Java’s
Array
Java’s
Iterable
Memory
Composition
Kotlin’s
Iterable;
Java 8
Streams
Rx
Observable
Javascript’s
Array
Java’s
Array
Java’s
Iterable
Memory
Composition
Kotlin’s
Iterable;
Java 8
Streams
Rx
Observable
Javascript’s
Array
declaratively
composable iterable
fun getPopulations(countries: Iterable<Country>)

= countries.map { it.population }
fun getPopulations(countries: Iterable<Country>)

= countries.map { it.population }
fun <T, R> Iterable<T>.map(transform: (T) -> R): List<R> {

val destination = ArrayList<R>(collectionSizeOrDefault(10))

for (item in this)

destination.add(transform(item))

return destination

}
fun <T, R> Iterable<T>.map(transform: (T) -> R): List<R> {

val destination = ArrayList<R>(collectionSizeOrDefault(10))

for (item in this)

destination.add(transform(item))

return destination

}
fun getTotalPopulationExcludingUs(countries: Iterable<Country>)

= countries

.filter { it.name == "United States" }

.map { it.population }

.reduce { total, currentPop -> total + currentPop }
fun getTotalPopulationExcludingUs(countries: Iterable<Country>)

= countries

.filter { it.name == "United States" }

.map { it.population }

.reduce { total, currentPop -> total + currentPop }
fun getTotalPopulationExcludingUs(countries: Iterable<Country>)

= countries

.filter { it.name == "United States" }

.map { it.population }

.reduce { total, currentPop -> total + currentPop }
fun getTotalPopulationExcludingUs(countries: Iterable<Country>)

= countries

.filter { it.name == "United States" }

.map { it.population }

.reduce { total, currentPop -> total + currentPop }
300e6 + 36e6 + 127e6 + …
declaratively
composable iterable
Java’s
Array
Java’s
Iterable
Memory
Composition
Kotlin’s
Iterable;
Java 8
Streams
Rx
Observable
Javascript’s
Array
Java’s
Array
Java’s
Iterable
Memory
Composition
Kotlin’s
Iterable;
Java 8
Streams
Rx
Observable
Javascript’s
Array
An Introduction to RxJava
declaratively
composable sequence
declaratively
composable sequence
button.setOnClickListener(new View.OnClickListener() {

@Override public void onClick(View v) {

trackClick();

}

});
for (int buttonClick : buttonClicks) {

trackClick();

}
button.setOnClickListener(new View.OnClickListener() {

@Override public void onClick(View v) {

trackClick();

}

});
for (int buttonClick : buttonClicks) {

trackClick();

}
button.forEachClick(new View.OnClickListener() {

@Override public void onClick(View v) {

trackClick();

}

});
for (int buttonClick : buttonClicks) {

trackClick();

}
clickSequence.forEachClick(new View.OnClickListener() {

@Override public void onClick(View v) {

trackClick();

}

});
clickSequence.forEachClick(new View.OnClickListener() {

@Override public void onClick(View v) {

trackClick();

}

});
clickObservable.subscribe(new Consumer<View> {

@Override public void accept(View v) {

trackClick();

}

});
declaratively
composable sequence
declaratively
composable sequence
declaratively composable
iterable
map
filter
reduce
map
filter
reduce
+ LinkedList
map
filter
reduce
+ LinkedList = 💥
How is the sequence
stored in memory?
Iterable
How is the sequence
stored in memory?
map
filter
reduce
map
filter
reduce
+ Async
map
filter
reduce
+ Async = 💥
Is the sequence
currently in memory?
Observable
Is the sequence
currently in memory?
Array
Array LinkedList
Array LinkedList
Iterable
Array LinkedList
Iterable
…
Array LinkedList
Iterable
…
Array LinkedList
Iterable
…
Sequence
Array LinkedList
Iterable
…
Sequence
Array LinkedList
Iterable
…
Observable
declaratively
composable sequence
An Introduction to RxJava
An Introduction to RxJava
[k, ke, key, keyn, keyno, keynot, keynote]
[results for ‘k’, results for ‘ke', results for ‘key’…]
for each result, update UI
makeQueryTextObservable(mSearchView)

.flatMap { s -> makeLoadObservable(s) }

.subscribe { cursor ->

mResultsAdapter.swapCursor(cursor)

}
makeQueryTextObservable(mSearchView)

.flatMap { s -> makeLoadObservable(s) }

.subscribe { cursor ->

mResultsAdapter.swapCursor(cursor)

}
[k, ke, key, keyn, keyno, keynot, keynote]
makeQueryTextObservable(mSearchView)

.flatMap { s -> makeLoadObservable(s) }

.subscribe { cursor ->

mResultsAdapter.swapCursor(cursor)

}
makeQueryTextObservable(mSearchView)

.flatMap { s -> makeLoadObservable(s) }

.subscribe { cursor ->

mResultsAdapter.swapCursor(cursor)

}
makeQueryTextObservable(mSearchView)

.flatMap { s -> makeLoadObservable(s) }

.subscribe { cursor ->

mResultsAdapter.swapCursor(cursor)

}
makeQueryTextObservable(mSearchView)

.flatMap { s -> makeLoadObservable(s) }

.subscribe { cursor ->

mResultsAdapter.swapCursor(cursor)

}
[results for ‘k’, results for ‘ke', results for ‘key’…]
makeQueryTextObservable(mSearchView)

.flatMap { s -> makeLoadObservable(s) }

.subscribe { cursor ->

mResultsAdapter.swapCursor(cursor)

}
makeQueryTextObservable(mSearchView)

.flatMap { s -> makeLoadObservable(s) }

.subscribe { cursor ->

mResultsAdapter.swapCursor(cursor)

}
makeQueryTextObservable(mSearchView)

.flatMap { s -> makeLoadObservable(s) }

.subscribe { cursor ->

mResultsAdapter.swapCursor(cursor)

}
makeQueryTextObservable(mSearchView)
.filter { s -> s.length > 3 }

.flatMap { s -> makeLoadObservable(s) }

.subscribe { cursor ->

mResultsAdapter.swapCursor(cursor)

}
makeQueryTextObservable(mSearchView)
.filter { s -> s.length > 3 }

.flatMap { s -> makeLoadObservable(s) }

.subscribe { cursor ->

mResultsAdapter.swapCursor(cursor)

}
[k, ke, key, keyn, keyno, keynot, keynote]
filter
[keyn, keyno, keynot, keynote]
makeQueryTextObservable(mSearchView)
.filter { s -> s.length > 3 }

.flatMap { s -> makeLoadObservable(s) }

.subscribe { cursor ->

mResultsAdapter.swapCursor(cursor)

}
makeQueryTextObservable(mSearchView!!)

.filter { s -> s.length > 3 }

.debounce(300, TimeUnit.MILLISECONDS)

.flatMap { s -> makeLoadObservable(s) }

.subscribe { cursor ->

mResultsAdapter!!.swapCursor(cursor)

}
makeQueryTextObservable(mSearchView!!)

.filter { s -> s.length > 3 }

.debounce(300, TimeUnit.MILLISECONDS)

.flatMap { s -> makeLoadObservable(s) }

.subscribe { cursor ->

mResultsAdapter!!.swapCursor(cursor)

}
makeQueryTextObservable(mSearchView!!)

.filter { s -> s.length > 3 }

.debounce(300, TimeUnit.MILLISECONDS)

.flatMap { s -> makeLoadObservable(s) }

.subscribe { cursor ->

mResultsAdapter!!.swapCursor(cursor)

}
makeQueryTextObservable(mSearchView!!)

.filter { s -> s.length > 3 }

.debounce(300, TimeUnit.MILLISECONDS)

.flatMap { s -> makeLoadObservable(s) }

.subscribeOn(Schedulers.io())

.observeOn(AndroidSchedulers.mainThread())

.subscribe { cursor ->

mResultsAdapter!!.swapCursor(cursor)

}
makeQueryTextObservable(mSearchView!!)

.filter { s -> s.length > 3 }

.debounce(300, TimeUnit.MILLISECONDS)

.flatMap { s -> makeLoadObservable(s) }

.subscribeOn(Schedulers.io())

.observeOn(AndroidSchedulers.mainThread())

.subscribe { cursor ->

mResultsAdapter!!.swapCursor(cursor)

}
makeQueryTextObservable(mSearchView!!)

.filter { s -> s.length > 3 }

.debounce(300, TimeUnit.MILLISECONDS)

.flatMap { s -> makeLoadObservable(s) }

.subscribeOn(Schedulers.io())

.observeOn(AndroidSchedulers.mainThread())

.subscribe { cursor ->

mResultsAdapter!!.swapCursor(cursor)

}
declaratively
composable sequence
An Introduction to RxJava
An Introduction to
RxJava
https://twitter.com/philosohacker
https://twitter.com/unikeytech

More Related Content

PDF
Java: Nie popełniaj tych błędów!
DOCX
WOTC_Import
DOCX
Spark_Documentation_Template1
PDF
Kotlin for Android Developers - Victor Kropp - Codemotion Rome 2018
PDF
Map/reduce, geospatial indexing, and other cool features (Kristina Chodorow)
DOCX
Java programs
PDF
First few months with Kotlin - Introduction through android examples
PPT
Chris Mc Glothen Sql Portfolio
Java: Nie popełniaj tych błędów!
WOTC_Import
Spark_Documentation_Template1
Kotlin for Android Developers - Victor Kropp - Codemotion Rome 2018
Map/reduce, geospatial indexing, and other cool features (Kristina Chodorow)
Java programs
First few months with Kotlin - Introduction through android examples
Chris Mc Glothen Sql Portfolio

What's hot (19)

PDF
Time Series Analysis by JavaScript LL matsuri 2013
KEY
Modern Algorithms and Data Structures - 1. Bloom Filters, Merkle Trees
PDF
The Ring programming language version 1.5.4 book - Part 68 of 185
PDF
Palestra collection google
PDF
Rのスコープとフレームと環境と
PDF
Open XKE - Big Data, Big Mess par Bertrand Dechoux
DOC
Laboratory activity 3 b3
PDF
Rデバッグあれこれ
PDF
The Ring programming language version 1.10 book - Part 80 of 212
DOCX
Ejemplo radio
PPT
Tricks
PDF
Scala dsls-dissecting-and-implementing-rogue
PDF
MySQL 5.7 NF – JSON Datatype 활용
PDF
AJUG April 2011 Raw hadoop example
PDF
多治見IT勉強会 Groovy Grails
PDF
Techtalk Rolling Scopes 2017 neural networks
PDF
The Ring programming language version 1.5.2 book - Part 76 of 181
PDF
PPT
computer notes - Data Structures - 21
Time Series Analysis by JavaScript LL matsuri 2013
Modern Algorithms and Data Structures - 1. Bloom Filters, Merkle Trees
The Ring programming language version 1.5.4 book - Part 68 of 185
Palestra collection google
Rのスコープとフレームと環境と
Open XKE - Big Data, Big Mess par Bertrand Dechoux
Laboratory activity 3 b3
Rデバッグあれこれ
The Ring programming language version 1.10 book - Part 80 of 212
Ejemplo radio
Tricks
Scala dsls-dissecting-and-implementing-rogue
MySQL 5.7 NF – JSON Datatype 활용
AJUG April 2011 Raw hadoop example
多治見IT勉強会 Groovy Grails
Techtalk Rolling Scopes 2017 neural networks
The Ring programming language version 1.5.2 book - Part 76 of 181
computer notes - Data Structures - 21
Ad

Similar to An Introduction to RxJava (20)

PDF
科特林λ學
PDF
Java Se next Generetion
PDF
Bologna Developer Zone - About Kotlin
PPTX
collectionsframework210616084411 (1).pptx
PPTX
Java Hands-On Workshop
PDF
Speed up the mobile development process
PDF
we using java dynamicArray package modellinearpub imp.pdf
DOCX
Collections framework
PPTX
Java Foundations: Maps, Lambda and Stream API
KEY
Processing & Dataviz
PPTX
Compose 3rd session.pptx
PDF
Miracle of std lib
PPTX
LJ_JAVA_FS_Collection.pptx
PPTX
18. Java associative arrays
PPT
Collections in Java
PPT
Collections
PDF
we using java code DynamicArrayjava Replace all .pdf
PDF
かとうの Kotlin 講座 こってり版
PPTX
Hello kotlin | An Event by DSC Unideb
科特林λ學
Java Se next Generetion
Bologna Developer Zone - About Kotlin
collectionsframework210616084411 (1).pptx
Java Hands-On Workshop
Speed up the mobile development process
we using java dynamicArray package modellinearpub imp.pdf
Collections framework
Java Foundations: Maps, Lambda and Stream API
Processing & Dataviz
Compose 3rd session.pptx
Miracle of std lib
LJ_JAVA_FS_Collection.pptx
18. Java associative arrays
Collections in Java
Collections
we using java code DynamicArrayjava Replace all .pdf
かとうの Kotlin 講座 こってり版
Hello kotlin | An Event by DSC Unideb
Ad

More from K. Matthew Dupree (9)

PDF
Generating Union types w/ Static Analysis
PDF
intro-to-metaprogramming-in-r.pdf
PDF
Intro To Gradient Descent in Javascript
PDF
Dagger 2, 2 years later
PDF
If Android Tests Could Talk
PDF
Writing testable android apps
PDF
Di and Dagger
PDF
Functional Testing for React Native Apps
PDF
Testable android apps
Generating Union types w/ Static Analysis
intro-to-metaprogramming-in-r.pdf
Intro To Gradient Descent in Javascript
Dagger 2, 2 years later
If Android Tests Could Talk
Writing testable android apps
Di and Dagger
Functional Testing for React Native Apps
Testable android apps

Recently uploaded (20)

PPTX
Cloud computing and distributed systems.
PPTX
Big Data Technologies - Introduction.pptx
PDF
Encapsulation theory and applications.pdf
PDF
Build a system with the filesystem maintained by OSTree @ COSCUP 2025
PDF
TokAI - TikTok AI Agent : The First AI Application That Analyzes 10,000+ Vira...
PPTX
Understanding_Digital_Forensics_Presentation.pptx
PPTX
Effective Security Operations Center (SOC) A Modern, Strategic, and Threat-In...
PDF
Empathic Computing: Creating Shared Understanding
PDF
Spectral efficient network and resource selection model in 5G networks
PPTX
Spectroscopy.pptx food analysis technology
PDF
KodekX | Application Modernization Development
PDF
Review of recent advances in non-invasive hemoglobin estimation
PDF
Peak of Data & AI Encore- AI for Metadata and Smarter Workflows
PPTX
ACSFv1EN-58255 AWS Academy Cloud Security Foundations.pptx
PDF
Dropbox Q2 2025 Financial Results & Investor Presentation
PDF
Reach Out and Touch Someone: Haptics and Empathic Computing
PDF
How UI/UX Design Impacts User Retention in Mobile Apps.pdf
PDF
Per capita expenditure prediction using model stacking based on satellite ima...
PDF
Blue Purple Modern Animated Computer Science Presentation.pdf.pdf
PDF
Architecting across the Boundaries of two Complex Domains - Healthcare & Tech...
Cloud computing and distributed systems.
Big Data Technologies - Introduction.pptx
Encapsulation theory and applications.pdf
Build a system with the filesystem maintained by OSTree @ COSCUP 2025
TokAI - TikTok AI Agent : The First AI Application That Analyzes 10,000+ Vira...
Understanding_Digital_Forensics_Presentation.pptx
Effective Security Operations Center (SOC) A Modern, Strategic, and Threat-In...
Empathic Computing: Creating Shared Understanding
Spectral efficient network and resource selection model in 5G networks
Spectroscopy.pptx food analysis technology
KodekX | Application Modernization Development
Review of recent advances in non-invasive hemoglobin estimation
Peak of Data & AI Encore- AI for Metadata and Smarter Workflows
ACSFv1EN-58255 AWS Academy Cloud Security Foundations.pptx
Dropbox Q2 2025 Financial Results & Investor Presentation
Reach Out and Touch Someone: Haptics and Empathic Computing
How UI/UX Design Impacts User Retention in Mobile Apps.pdf
Per capita expenditure prediction using model stacking based on satellite ima...
Blue Purple Modern Animated Computer Science Presentation.pdf.pdf
Architecting across the Boundaries of two Complex Domains - Healthcare & Tech...

An Introduction to RxJava