SlideShare a Scribd company logo
Reactive Programming on
Android
Erik Meijer - Rx .NET Ben Christensen - Rx Java
Introduction
Getting Started
Pull X Push
Getting Started
RxAndroid Dependency
compile 'io.reactivex:rxandroid:0.24.0'
Brings all you need
RXJava and RXAndroid
Anatomy
Observable.from(doLongNetworkOp())
.subcribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(getObserver())
1. Observable
2. Observer
3. Schedulers
Anatomy
2. Observer.subscribe(getObserver())
private Observer<String> getObserver(){
new Observer<String>() {
@Override
public void onCompleted() { … }
@Override
public void onError(Throwable e) { … }
@Override
public void onNext(String string) { … }
};
}
Anatomy
Observable.from(doLongNetworkOp())
.subcribeOn(schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(getObserver())
1. Observable
2. Observer
3. Schedulers
Observable Observer
4. Subscription
=
Subscription subscription = Observable.from(doLongNetworkOp())
.subcribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(getObserver())
1. subscription.unsubscribe()
2. CompositeSubscription compSubscription;
compSubscription.add(subscription);
3. compSubscription.unsubscribe()
Anatomy
Getting Started - Producer and Consumer
Observable Observer
Emits
item1
item2
item3
Uses item 1
Uses item 2
Uses item 3
onNext(item1)
onNext(item2)
onNext(item3)
no more items onCompleted() complete
Ops! Error… onError(error) Handles error
AsyncTask → Observable/Observer
new FindUserTask().execute(userId);
private class FindUserTask extends AsyncTask<Long, Void, Void>{
@Override
protected void onPreExecute() {
showLoading();
}
@Override
protected User doInBackground(Long... params){
return userService.findUserById(params[0]);
}
@Override
protected void onPostExecute(User user){
mUserName.setText(user.getName());
hideLoading();
}
}
Observable
.just(userId)
.flatMap(new Func1<Long, Observable<User>>(){
@Override
public Observable<User> call(Long userId){
return Observable.just(userService.findUserById(userId);
})
.subscribeOn(Schedulers.io())
.subscribe(
new Observer<User>() {
@Override
public void onCompleted() {
Log.d(TAG, "onCompleted");
hideLoading();
}
@Override
public void onError(Throwable e) {
Log.d(TAG, "onError " + e.getMessage());
}
@Override
public void onNext(User user) {
Log.d(TAG, "onNext");
mUserName.setText(user.getName());
}
});
android.view.ViewRootImpl$CalledFromWrongThreadException: Only the original thread that created a
view hierarchy can touch its views.
Ops! Something went wrong
AsyncTask → Observable/Observer
Observable.just(userId)
.flatMap(new Func1<Long, Observable<User>>(){
@Override
public Observable<User> call(Long userId){
return Observable.just(service.findUserById(userId);
}
})
.subscribe(
new Observer<User>() {
@Override
public void onCompleted() { … }
@Override
public void onError(Throwable e) { … }
@Override
public void onNext(User user) { … }
});
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
Operators
● Create
● From
● Just
● Filter
● Map
● Concat
● Many others...
Observable.create(new Observable.OnSubscribe<Integer>() {
@Override
public void call(Subscriber<? super Integer> observer) {
try {
if (!observer.isUnsubscribed()) {
for (int i = 1; i < 5; i++) {
observer.onNext(i);
}
observer.onCompleted();
}
} catch (Exception e) {
observer.onError(e);
}
}
} );
Operators - Create
Integer[] items = { 0, 1, 2, 3, 4, 5 };
Observable myObservable = Observable.from(items);
Operators - From
Observable.just(1, 2, 3)
.subscribe(new Subscriber<Integer>() {
@Override
public void onNext(Integer item) {
System.out.println(“onNext:” + item);
}
@Override
public void onError(Throwable error) {
System.err.println(“Error: ” + error.getMessage());
}
@Override
public void omCompleted() {
System.out.println("Sequence complete. ”);
}
});
Operators - Just
Observable.just(1, 2, 3, 4)
.filter(new Func1<Integer, Boolean>(){
@Override
public Boolean call(Integer number){
return (number < 4);
}
})
.subscribe(
new Subscriber<Integer>() {
@Override
public void onCompleted() { Log.d(TAG, "Sequence complete"); }
@Override
public void onError(Throwable e) { Log.d(TAG, "onError"); }
@Override
public void onNext(Integer number) { Log.d(TAG, "onNext " + number); }
});
onNext 1
onNext 2
onNext 3
Sequence complete
Operators - Filter
Operators - Map
Operators - Concat
http://reactivex.io/documentation/operators.html
http://rxmarbles.com
A Decision Tree of Observable Operators
I want to emit only certain items from an Observable
by filtering out those that do not match some predicate Filter
that is, only the first item First
that is, only the first items Take
that is, only the last item Last
Operators - Many Others
Questions ?

More Related Content

What's hot (19)

PDF
OSGi World Congress Workshop Exercise - P Kriens
mfrancis
 
PDF
JVM Mechanics: Understanding the JIT's Tricks
Doug Hawkins
 
PDF
RxJava on Android
Dustin Graham
 
PDF
Java Performance Puzzlers
Doug Hawkins
 
PPTX
An Introduction to RxJava
Sanjay Acharya
 
PPTX
Rxandroid
Thinh Thanh
 
PDF
The Ring programming language version 1.8 book - Part 88 of 202
Mahmoud Samir Fayed
 
PDF
JVM Mechanics
Doug Hawkins
 
PDF
Saving lives with rx java
Shahar Barsheshet
 
PDF
OSMC 2012 | Neues in Nagios 4.0 by Andreas Ericsson
NETWAYS
 
PDF
Practical RxJava for Android
Tomáš Kypta
 
PDF
RxJava for Android - GDG DevFest Ukraine 2015
Constantine Mars
 
PPTX
Unit testing CourseSites Apache Filter
Wayan Wira
 
PDF
The Ring programming language version 1.5.4 book - Part 79 of 185
Mahmoud Samir Fayed
 
KEY
Agile Iphone Development
Giordano Scalzo
 
PDF
RxJava applied [JavaDay Kyiv 2016]
Igor Lozynskyi
 
PPTX
RxJava Applied
Igor Lozynskyi
 
PDF
Mutation @ Spotify
STAMP Project
 
ODP
Java Generics
Carol McDonald
 
OSGi World Congress Workshop Exercise - P Kriens
mfrancis
 
JVM Mechanics: Understanding the JIT's Tricks
Doug Hawkins
 
RxJava on Android
Dustin Graham
 
Java Performance Puzzlers
Doug Hawkins
 
An Introduction to RxJava
Sanjay Acharya
 
Rxandroid
Thinh Thanh
 
The Ring programming language version 1.8 book - Part 88 of 202
Mahmoud Samir Fayed
 
JVM Mechanics
Doug Hawkins
 
Saving lives with rx java
Shahar Barsheshet
 
OSMC 2012 | Neues in Nagios 4.0 by Andreas Ericsson
NETWAYS
 
Practical RxJava for Android
Tomáš Kypta
 
RxJava for Android - GDG DevFest Ukraine 2015
Constantine Mars
 
Unit testing CourseSites Apache Filter
Wayan Wira
 
The Ring programming language version 1.5.4 book - Part 79 of 185
Mahmoud Samir Fayed
 
Agile Iphone Development
Giordano Scalzo
 
RxJava applied [JavaDay Kyiv 2016]
Igor Lozynskyi
 
RxJava Applied
Igor Lozynskyi
 
Mutation @ Spotify
STAMP Project
 
Java Generics
Carol McDonald
 

Similar to Reactive Programming on Android (20)

PPTX
RxAndroid
Thinh Thanh
 
PPTX
2 презентация rx java+android
STEP Computer Academy (Zaporozhye)
 
PPTX
Intro to Functional Programming with RxJava
Mike Nakhimovich
 
PDF
How to Think in RxJava Before Reacting
IndicThreads
 
PDF
RxJava@Android
Maxim Volgin
 
PDF
Reactive programming on Android
Tomáš Kypta
 
PDF
Reactive programming on Android
Tomáš Kypta
 
PPTX
Introduction to RxJava on Android
Chris Arriola
 
PPTX
Introduction to Reactive programming
Dwi Randy Herdinanto
 
PDF
Streamlining with rx
Akhil Dad
 
PPTX
Rx java in action
Pratama Nur Wijaya
 
PDF
RxJava@DAUG
Maxim Volgin
 
PDF
The Mayans Lost Guide to RxJava on Android
Fernando Cejas
 
PPTX
Rx for Android & iOS by Harin Trivedi
harintrivedi
 
PDF
RxJava in practice
Javier Gamarra
 
PDF
Streams, Streams Everywhere! An Introduction to Rx
Andrzej Sitek
 
PDF
RxJava - introduction & design
allegro.tech
 
PPTX
Rx presentation
Ali Mahfud
 
PDF
Reactive Functional Programming with Java 8 on Android N
Shipeng Xu
 
PPTX
Reactive Programming on Android - RxAndroid - RxJava
Ali Muzaffar
 
RxAndroid
Thinh Thanh
 
2 презентация rx java+android
STEP Computer Academy (Zaporozhye)
 
Intro to Functional Programming with RxJava
Mike Nakhimovich
 
How to Think in RxJava Before Reacting
IndicThreads
 
RxJava@Android
Maxim Volgin
 
Reactive programming on Android
Tomáš Kypta
 
Reactive programming on Android
Tomáš Kypta
 
Introduction to RxJava on Android
Chris Arriola
 
Introduction to Reactive programming
Dwi Randy Herdinanto
 
Streamlining with rx
Akhil Dad
 
Rx java in action
Pratama Nur Wijaya
 
RxJava@DAUG
Maxim Volgin
 
The Mayans Lost Guide to RxJava on Android
Fernando Cejas
 
Rx for Android & iOS by Harin Trivedi
harintrivedi
 
RxJava in practice
Javier Gamarra
 
Streams, Streams Everywhere! An Introduction to Rx
Andrzej Sitek
 
RxJava - introduction & design
allegro.tech
 
Rx presentation
Ali Mahfud
 
Reactive Functional Programming with Java 8 on Android N
Shipeng Xu
 
Reactive Programming on Android - RxAndroid - RxJava
Ali Muzaffar
 
Ad

Recently uploaded (20)

PPTX
GitOps_Without_K8s_Training_detailed git repository
DanialHabibi2
 
PPTX
Presentation 2.pptx AI-powered home security systems Secure-by-design IoT fr...
SoundaryaBC2
 
PPTX
Arduino Based Gas Leakage Detector Project
CircuitDigest
 
PDF
MAD Unit - 2 Activity and Fragment Management in Android (Diploma IT)
JappanMavani
 
PPTX
Evaluation and thermal analysis of shell and tube heat exchanger as per requi...
shahveer210504
 
PPTX
Heart Bleed Bug - A case study (Course: Cryptography and Network Security)
Adri Jovin
 
PDF
Pressure Measurement training for engineers and Technicians
AIESOLUTIONS
 
PPTX
Element 11. ELECTRICITY safety and hazards
merrandomohandas
 
PDF
International Journal of Information Technology Convergence and services (IJI...
ijitcsjournal4
 
PPTX
Worm gear strength and wear calculation as per standard VB Bhandari Databook.
shahveer210504
 
PPTX
265587293-NFPA 101 Life safety code-PPT-1.pptx
chandermwason
 
DOCX
8th International Conference on Electrical Engineering (ELEN 2025)
elelijjournal653
 
PPTX
Big Data and Data Science hype .pptx
SUNEEL37
 
PPTX
Damage of stability of a ship and how its change .pptx
ehamadulhaque
 
PPT
PPT2_Metal formingMECHANICALENGINEEIRNG .ppt
Praveen Kumar
 
DOC
MRRS Strength and Durability of Concrete
CivilMythili
 
PDF
Reasons for the succes of MENARD PRESSUREMETER.pdf
majdiamz
 
PDF
Basic_Concepts_in_Clinical_Biochemistry_2018كيمياء_عملي.pdf
AdelLoin
 
PDF
Design Thinking basics for Engineers.pdf
CMR University
 
PPTX
Lecture 1 Shell and Tube Heat exchanger-1.pptx
mailforillegalwork
 
GitOps_Without_K8s_Training_detailed git repository
DanialHabibi2
 
Presentation 2.pptx AI-powered home security systems Secure-by-design IoT fr...
SoundaryaBC2
 
Arduino Based Gas Leakage Detector Project
CircuitDigest
 
MAD Unit - 2 Activity and Fragment Management in Android (Diploma IT)
JappanMavani
 
Evaluation and thermal analysis of shell and tube heat exchanger as per requi...
shahveer210504
 
Heart Bleed Bug - A case study (Course: Cryptography and Network Security)
Adri Jovin
 
Pressure Measurement training for engineers and Technicians
AIESOLUTIONS
 
Element 11. ELECTRICITY safety and hazards
merrandomohandas
 
International Journal of Information Technology Convergence and services (IJI...
ijitcsjournal4
 
Worm gear strength and wear calculation as per standard VB Bhandari Databook.
shahveer210504
 
265587293-NFPA 101 Life safety code-PPT-1.pptx
chandermwason
 
8th International Conference on Electrical Engineering (ELEN 2025)
elelijjournal653
 
Big Data and Data Science hype .pptx
SUNEEL37
 
Damage of stability of a ship and how its change .pptx
ehamadulhaque
 
PPT2_Metal formingMECHANICALENGINEEIRNG .ppt
Praveen Kumar
 
MRRS Strength and Durability of Concrete
CivilMythili
 
Reasons for the succes of MENARD PRESSUREMETER.pdf
majdiamz
 
Basic_Concepts_in_Clinical_Biochemistry_2018كيمياء_عملي.pdf
AdelLoin
 
Design Thinking basics for Engineers.pdf
CMR University
 
Lecture 1 Shell and Tube Heat exchanger-1.pptx
mailforillegalwork
 
Ad

Reactive Programming on Android

Editor's Notes

  • #20: emit the emissions from two or more Observables without interleaving them