SlideShare une entreprise Scribd logo
1  sur  26
Télécharger pour lire hors ligne
RxJava
RxAndroid
VIVINT, October 2015
Declarative vs. Imperative Programming
➤ Imperative Programming: telling the machine how to do something,
and as a result what you want to happen will happen.
➤ Declarative Programming: telling the machine what you would like
to happen, and let the computer figure out how to do it.
var numbers = [1,2,3,4,5]
var doubled = []
for(var i = 0; i < numbers.length; i++) {
var newNumber = numbers[i] * 2
doubled.push(newNumber)
}
console.log(doubled) //=> [2,4,6,8,10]
var numbers = [1,2,3,4,5]
var total = 0
for(var i = 0; i < numbers.length; i++) {
total += numbers[i]
}
console.log(total) //=> 15
var numbers = [1,2,3,4,5]
var doubled = numbers.map(function(n) {
return n * 2
})
console.log(doubled) //=> [2,4,6,8,10]
var numbers = [1,2,3,4,5]
var total = numbers.reduce(function(sum,
n) {
return sum + n
});
console.log(total) //=> 15
Think about spreadsheets
A cell does not represent a value, but a potential stream of values.

That stream can be split into many streams, combined or fed into other streams.
There are high-order functions that can consume these streams:

‘AVERAGE’, ‘SUM’,’SUMIF’,’POWER’, etc…
Functional Reactive Programming
Savings Percent 0.2 Monthly Wages 1600
Hourly Wage 10 Annual Wages 19200
Hours Worked/Wk 40 Annual Savings 3840
Weekly Wages 400
Savings Percent 0.4 Monthly Wages 3200
Hourly Wage 20 Annual Wages 38400
Hours Worked/Wk 40 Annual Savings 15360
Weekly Wages 800
What is RxJava?
➤ Observables & Subscribers
➤ High-order functions
➤ Schedulers
Observer Contract
Observable.just(1,2,3,4,5)
.subscribe(new Subscriber<Integer>() {
@Override
public void onCompleted() {
Log.d("Test", "sequence completed");
}
@Override
public void onError(Throwable e) {
Log.e("Test", "got error: " + e.getMessage());
}
@Override
public void onNext(Integer integer) {
Log.d("Test", "got int: " + integer);
}
});
The contract is very simple: onNext,onError,onCompleted
High-order functions
➤ filter
➤ map
➤ cast
➤ buffer
➤ debounce
➤ merge
➤ zip
➤ combineLatest
➤ flatMap
A few common examples:
Filter
Map
Cast
Buffer
Debounce
Merge
Zip
CombineLatest
FlatMap
Schedulers
➤ computation - for computation work
➤ immediate - current thread
➤ io - IO-bound work
➤ newThread - new thread for each unit
➤ test - useful for debugging
➤ trampoline - current thread, after current work
➤ AndroidSchedulers.mainThread (RxAndroid)
Consistent with the purpose of the
high-order functions, RxJava’s
schedulers remove the concern of how
code gets run on different threads
and allows the developer to simply
declare which scheduler should run
which units of work.
Example Scheduler Usage
Observable
.just(1)
.delay(10, TimeUnit.SECONDS, Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(new Subscriber<Integer>() {
@Override
public void onCompleted() {
}
@Override
public void onError(Throwable e) {
}
@Override
public void onNext(Integer integer) {
Log.d("Test", "this is on the main thread!");
}
});
RxJava + Schedulers vs AsyncTask
➤ Easier to use
➤ More effective error handling (built-in)
➤ Use of high-order functions
➤ Easily cancelable
➤ Easily retry-able
➤ Testable
➤ Greater control over concurrency
➤ AsyncTask has just 2 options, ThreadPoolExecutor or not and you can’t control what thread the onPostExecute
fires on.
representativeApi.representativesByZipCode(zipCode)
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.toList()
.retry(2)
.subscribe(onRepresentativesReceived());
A compelling RxJava Example
RxJava on Android
➤ RxAndroid - minimum base
➤ RxLifecycle - easy lifecycle
handling
➤ RxBinding - easily create
observables from Android UI
widgets
As of this writing, RxAndroid has
had a lot of recent changes. Formerly,
it contained nearly every useful thing
required to build RxJava apps in
Android. It has now been broken up
into several constituent pieces.
RxLifecycle
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Observable
.interval(1, TimeUnit.SECONDS, Schedulers.io())
.compose(this.<Long>bindUntilEvent(ActivityEvent.DESTROY))
.observeOn(AndroidSchedulers.mainThread())
.subscribe(new Action1<Long>() {
@Override
public void call(Long aLong) {
Log.d("Test", "logging stuff until destroyed");
}
});
}
Makes it easy to keep updates happening within safe timeframes
RxBinding
Observable<Double> hourlyWageObservable = RxTextView.textChanges(_hourlyWage)
.map(new Func1<CharSequence, Double>() {
@Override
public Double call(CharSequence charSequence) {
try {
return Double.parseDouble(charSequence.toString());
} catch (NumberFormatException e) {
return 0D;
}
}
});
Makes it easy to create Observables from Android UI widgets
RxJava Challenges
➤ No lambdas on Android

(requires Java 8)
➤ Lots of extra code, but
nearly negated by solid
auto-complete tools
➤ Can be overcome with
retrolambda but it can be
fickle
➤ Steep learning curve
➤ Easy traditional solutions
can seem difficult to
perform with RxJava at first
Useful compatible libraries
➤ SqlBrite
➤ Retrofit - example
➤ RxPreferences
Testing
➤ One Interesting Article
➤ Another Interesting Article
Sample Project
➤ RxAndroid-Sample
➤ Use of RxAndroid,
RxLifecycle, RxBinding
➤ Use of Retrofit with RxJava
➤ Example of converting non-
RxJava code to work with
RxJava
➤ More…
Final Words
RxJava is not about writing less code or doing things that were
impossible before. It’s about focusing more lines of code toward
your application’s actual objectives. It helps with this by
removing all the tedious, generic coding that we have to do
every day: allowing the solved problems stay solved while we
move on to more important things. When we can reduce the
number of wrote, for-loop collection manipulation routines we
can do in our sleep we can spend more time thinking about and
solving the real problems.

Contenu connexe

Tendances

Reactive programming with RxAndroid
Reactive programming with RxAndroidReactive programming with RxAndroid
Reactive programming with RxAndroidSavvycom Savvycom
 
Reactive Android: RxJava and beyond
Reactive Android: RxJava and beyondReactive Android: RxJava and beyond
Reactive Android: RxJava and beyondFabio Tiriticco
 
Introduction to RxJS
Introduction to RxJSIntroduction to RxJS
Introduction to RxJSBrainhub
 
Reactive programming with RxJava
Reactive programming with RxJavaReactive programming with RxJava
Reactive programming with RxJavaJobaer Chowdhury
 
Building Scalable Stateless Applications with RxJava
Building Scalable Stateless Applications with RxJavaBuilding Scalable Stateless Applications with RxJava
Building Scalable Stateless Applications with RxJavaRick Warren
 
Practical RxJava for Android
Practical RxJava for AndroidPractical RxJava for Android
Practical RxJava for AndroidTomáš Kypta
 
My Gentle Introduction to RxJS
My Gentle Introduction to RxJSMy Gentle Introduction to RxJS
My Gentle Introduction to RxJSMattia Occhiuto
 
Introduction to Retrofit and RxJava
Introduction to Retrofit and RxJavaIntroduction to Retrofit and RxJava
Introduction to Retrofit and RxJavaFabio Collini
 
Streams, Streams Everywhere! An Introduction to Rx
Streams, Streams Everywhere! An Introduction to RxStreams, Streams Everywhere! An Introduction to Rx
Streams, Streams Everywhere! An Introduction to RxAndrzej Sitek
 
RxJava applied [JavaDay Kyiv 2016]
RxJava applied [JavaDay Kyiv 2016]RxJava applied [JavaDay Kyiv 2016]
RxJava applied [JavaDay Kyiv 2016]Igor Lozynskyi
 
Functional Reactive Programming / Compositional Event Systems
Functional Reactive Programming / Compositional Event SystemsFunctional Reactive Programming / Compositional Event Systems
Functional Reactive Programming / Compositional Event SystemsLeonardo Borges
 
Reactive programming on Android
Reactive programming on AndroidReactive programming on Android
Reactive programming on AndroidTomáš Kypta
 
Non Blocking I/O for Everyone with RxJava
Non Blocking I/O for Everyone with RxJavaNon Blocking I/O for Everyone with RxJava
Non Blocking I/O for Everyone with RxJavaFrank Lyaruu
 
RxJava 2.0 介紹
RxJava 2.0 介紹RxJava 2.0 介紹
RxJava 2.0 介紹Kros Huang
 

Tendances (20)

Reactive programming with RxAndroid
Reactive programming with RxAndroidReactive programming with RxAndroid
Reactive programming with RxAndroid
 
Reactive Android: RxJava and beyond
Reactive Android: RxJava and beyondReactive Android: RxJava and beyond
Reactive Android: RxJava and beyond
 
Introduction to RxJS
Introduction to RxJSIntroduction to RxJS
Introduction to RxJS
 
Reactive programming with RxJava
Reactive programming with RxJavaReactive programming with RxJava
Reactive programming with RxJava
 
Building Scalable Stateless Applications with RxJava
Building Scalable Stateless Applications with RxJavaBuilding Scalable Stateless Applications with RxJava
Building Scalable Stateless Applications with RxJava
 
Practical RxJava for Android
Practical RxJava for AndroidPractical RxJava for Android
Practical RxJava for Android
 
RxJava Applied
RxJava AppliedRxJava Applied
RxJava Applied
 
My Gentle Introduction to RxJS
My Gentle Introduction to RxJSMy Gentle Introduction to RxJS
My Gentle Introduction to RxJS
 
Reactive Java (GeeCON 2014)
Reactive Java (GeeCON 2014)Reactive Java (GeeCON 2014)
Reactive Java (GeeCON 2014)
 
Introduction to Reactive Java
Introduction to Reactive JavaIntroduction to Reactive Java
Introduction to Reactive Java
 
rx-java-presentation
rx-java-presentationrx-java-presentation
rx-java-presentation
 
RxJava@Android
RxJava@AndroidRxJava@Android
RxJava@Android
 
Introduction to Retrofit and RxJava
Introduction to Retrofit and RxJavaIntroduction to Retrofit and RxJava
Introduction to Retrofit and RxJava
 
Streams, Streams Everywhere! An Introduction to Rx
Streams, Streams Everywhere! An Introduction to RxStreams, Streams Everywhere! An Introduction to Rx
Streams, Streams Everywhere! An Introduction to Rx
 
RxJava applied [JavaDay Kyiv 2016]
RxJava applied [JavaDay Kyiv 2016]RxJava applied [JavaDay Kyiv 2016]
RxJava applied [JavaDay Kyiv 2016]
 
Rx java in action
Rx java in actionRx java in action
Rx java in action
 
Functional Reactive Programming / Compositional Event Systems
Functional Reactive Programming / Compositional Event SystemsFunctional Reactive Programming / Compositional Event Systems
Functional Reactive Programming / Compositional Event Systems
 
Reactive programming on Android
Reactive programming on AndroidReactive programming on Android
Reactive programming on Android
 
Non Blocking I/O for Everyone with RxJava
Non Blocking I/O for Everyone with RxJavaNon Blocking I/O for Everyone with RxJava
Non Blocking I/O for Everyone with RxJava
 
RxJava 2.0 介紹
RxJava 2.0 介紹RxJava 2.0 介紹
RxJava 2.0 介紹
 

En vedette

RxJava for Android - GDG and DataArt
RxJava for Android - GDG and DataArtRxJava for Android - GDG and DataArt
RxJava for Android - GDG and DataArtConstantine Mars
 
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 AndroidFernando Cejas
 
RxJava on Android
RxJava on AndroidRxJava on Android
RxJava on Androidyo_waka
 
Introduction to rx java for android
Introduction to rx java for androidIntroduction to rx java for android
Introduction to rx java for androidEsa Firman
 
Rxjava 介紹與 Android 中的 RxJava
Rxjava 介紹與 Android 中的 RxJavaRxjava 介紹與 Android 中的 RxJava
Rxjava 介紹與 Android 中的 RxJavaKros Huang
 
Rx Java architecture
Rx Java architectureRx Java architecture
Rx Java architecturee-Legion
 
Headless fragments in Android
Headless fragments in AndroidHeadless fragments in Android
Headless fragments in AndroidAli Muzaffar
 

En vedette (9)

2 презентация rx java+android
2 презентация rx java+android2 презентация rx java+android
2 презентация rx java+android
 
RxJava for Android - GDG and DataArt
RxJava for Android - GDG and DataArtRxJava for Android - GDG and DataArt
RxJava for Android - GDG and DataArt
 
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
 
Android concurrency
Android concurrencyAndroid concurrency
Android concurrency
 
RxJava on Android
RxJava on AndroidRxJava on Android
RxJava on Android
 
Introduction to rx java for android
Introduction to rx java for androidIntroduction to rx java for android
Introduction to rx java for android
 
Rxjava 介紹與 Android 中的 RxJava
Rxjava 介紹與 Android 中的 RxJavaRxjava 介紹與 Android 中的 RxJava
Rxjava 介紹與 Android 中的 RxJava
 
Rx Java architecture
Rx Java architectureRx Java architecture
Rx Java architecture
 
Headless fragments in Android
Headless fragments in AndroidHeadless fragments in Android
Headless fragments in Android
 

Similaire à RxJava on Android

Jdk 7 4-forkjoin
Jdk 7 4-forkjoinJdk 7 4-forkjoin
Jdk 7 4-forkjoinknight1128
 
Functional Reactive Programming with RxJS
Functional Reactive Programming with RxJSFunctional Reactive Programming with RxJS
Functional Reactive Programming with RxJSstefanmayer13
 
ClojureScript loves React, DomCode May 26 2015
ClojureScript loves React, DomCode May 26 2015ClojureScript loves React, DomCode May 26 2015
ClojureScript loves React, DomCode May 26 2015Michiel Borkent
 
Yevhen Tatarynov "From POC to High-Performance .NET applications"
Yevhen Tatarynov "From POC to High-Performance .NET applications"Yevhen Tatarynov "From POC to High-Performance .NET applications"
Yevhen Tatarynov "From POC to High-Performance .NET applications"LogeekNightUkraine
 
Integration-Monday-Stateful-Programming-Models-Serverless-Functions
Integration-Monday-Stateful-Programming-Models-Serverless-FunctionsIntegration-Monday-Stateful-Programming-Models-Serverless-Functions
Integration-Monday-Stateful-Programming-Models-Serverless-FunctionsBizTalk360
 
Beyond Breakpoints: A Tour of Dynamic Analysis
Beyond Breakpoints: A Tour of Dynamic AnalysisBeyond Breakpoints: A Tour of Dynamic Analysis
Beyond Breakpoints: A Tour of Dynamic AnalysisFastly
 
GDG Jakarta Meetup - Streaming Analytics With Apache Beam
GDG Jakarta Meetup - Streaming Analytics With Apache BeamGDG Jakarta Meetup - Streaming Analytics With Apache Beam
GDG Jakarta Meetup - Streaming Analytics With Apache BeamImre Nagi
 
用 Go 語言打造多台機器 Scale 架構
用 Go 語言打造多台機器 Scale 架構用 Go 語言打造多台機器 Scale 架構
用 Go 語言打造多台機器 Scale 架構Bo-Yi Wu
 
Functional Reactive Programming in JavaScript
Functional Reactive Programming in JavaScriptFunctional Reactive Programming in JavaScript
Functional Reactive Programming in JavaScriptzupzup.org
 
CS101- Introduction to Computing- Lecture 35
CS101- Introduction to Computing- Lecture 35CS101- Introduction to Computing- Lecture 35
CS101- Introduction to Computing- Lecture 35Bilal Ahmed
 
オープンデータを使ったモバイルアプリ開発(応用編)
オープンデータを使ったモバイルアプリ開発(応用編)オープンデータを使ったモバイルアプリ開発(応用編)
オープンデータを使ったモバイルアプリ開発(応用編)Takayuki Goto
 
Parallel Programming With Dot Net
Parallel Programming With Dot NetParallel Programming With Dot Net
Parallel Programming With Dot NetNeeraj Kaushik
 
Help Needed!UNIX Shell and History Feature This project consists.pdf
Help Needed!UNIX Shell and History Feature This project consists.pdfHelp Needed!UNIX Shell and History Feature This project consists.pdf
Help Needed!UNIX Shell and History Feature This project consists.pdfmohdjakirfb
 
Stranger in These Parts. A Hired Gun in the JS Corral (JSConf US 2012)
Stranger in These Parts. A Hired Gun in the JS Corral (JSConf US 2012)Stranger in These Parts. A Hired Gun in the JS Corral (JSConf US 2012)
Stranger in These Parts. A Hired Gun in the JS Corral (JSConf US 2012)Igalia
 

Similaire à RxJava on Android (20)

Jdk 7 4-forkjoin
Jdk 7 4-forkjoinJdk 7 4-forkjoin
Jdk 7 4-forkjoin
 
Functional Reactive Programming with RxJS
Functional Reactive Programming with RxJSFunctional Reactive Programming with RxJS
Functional Reactive Programming with RxJS
 
Rx workshop
Rx workshopRx workshop
Rx workshop
 
ClojureScript loves React, DomCode May 26 2015
ClojureScript loves React, DomCode May 26 2015ClojureScript loves React, DomCode May 26 2015
ClojureScript loves React, DomCode May 26 2015
 
Yevhen Tatarynov "From POC to High-Performance .NET applications"
Yevhen Tatarynov "From POC to High-Performance .NET applications"Yevhen Tatarynov "From POC to High-Performance .NET applications"
Yevhen Tatarynov "From POC to High-Performance .NET applications"
 
Integration-Monday-Stateful-Programming-Models-Serverless-Functions
Integration-Monday-Stateful-Programming-Models-Serverless-FunctionsIntegration-Monday-Stateful-Programming-Models-Serverless-Functions
Integration-Monday-Stateful-Programming-Models-Serverless-Functions
 
Beyond Breakpoints: A Tour of Dynamic Analysis
Beyond Breakpoints: A Tour of Dynamic AnalysisBeyond Breakpoints: A Tour of Dynamic Analysis
Beyond Breakpoints: A Tour of Dynamic Analysis
 
GDG Jakarta Meetup - Streaming Analytics With Apache Beam
GDG Jakarta Meetup - Streaming Analytics With Apache BeamGDG Jakarta Meetup - Streaming Analytics With Apache Beam
GDG Jakarta Meetup - Streaming Analytics With Apache Beam
 
Golang dot-testing-lite
Golang dot-testing-liteGolang dot-testing-lite
Golang dot-testing-lite
 
用 Go 語言打造多台機器 Scale 架構
用 Go 語言打造多台機器 Scale 架構用 Go 語言打造多台機器 Scale 架構
用 Go 語言打造多台機器 Scale 架構
 
Functional Reactive Programming in JavaScript
Functional Reactive Programming in JavaScriptFunctional Reactive Programming in JavaScript
Functional Reactive Programming in JavaScript
 
CS101- Introduction to Computing- Lecture 35
CS101- Introduction to Computing- Lecture 35CS101- Introduction to Computing- Lecture 35
CS101- Introduction to Computing- Lecture 35
 
Saving lives with rx java
Saving lives with rx javaSaving lives with rx java
Saving lives with rx java
 
オープンデータを使ったモバイルアプリ開発(応用編)
オープンデータを使ったモバイルアプリ開発(応用編)オープンデータを使ったモバイルアプリ開発(応用編)
オープンデータを使ったモバイルアプリ開発(応用編)
 
Von neumann workers
Von neumann workersVon neumann workers
Von neumann workers
 
Parallel Programming With Dot Net
Parallel Programming With Dot NetParallel Programming With Dot Net
Parallel Programming With Dot Net
 
Help Needed!UNIX Shell and History Feature This project consists.pdf
Help Needed!UNIX Shell and History Feature This project consists.pdfHelp Needed!UNIX Shell and History Feature This project consists.pdf
Help Needed!UNIX Shell and History Feature This project consists.pdf
 
Twins: OOP and FP
Twins: OOP and FPTwins: OOP and FP
Twins: OOP and FP
 
Microkernel Development
Microkernel DevelopmentMicrokernel Development
Microkernel Development
 
Stranger in These Parts. A Hired Gun in the JS Corral (JSConf US 2012)
Stranger in These Parts. A Hired Gun in the JS Corral (JSConf US 2012)Stranger in These Parts. A Hired Gun in the JS Corral (JSConf US 2012)
Stranger in These Parts. A Hired Gun in the JS Corral (JSConf US 2012)
 

Dernier

The Most Attractive Pune Call Girls Manchar 8250192130 Will You Miss This Cha...
The Most Attractive Pune Call Girls Manchar 8250192130 Will You Miss This Cha...The Most Attractive Pune Call Girls Manchar 8250192130 Will You Miss This Cha...
The Most Attractive Pune Call Girls Manchar 8250192130 Will You Miss This Cha...ranjana rawat
 
UNIT-IFLUID PROPERTIES & FLOW CHARACTERISTICS
UNIT-IFLUID PROPERTIES & FLOW CHARACTERISTICSUNIT-IFLUID PROPERTIES & FLOW CHARACTERISTICS
UNIT-IFLUID PROPERTIES & FLOW CHARACTERISTICSrknatarajan
 
CCS335 _ Neural Networks and Deep Learning Laboratory_Lab Complete Record
CCS335 _ Neural Networks and Deep Learning Laboratory_Lab Complete RecordCCS335 _ Neural Networks and Deep Learning Laboratory_Lab Complete Record
CCS335 _ Neural Networks and Deep Learning Laboratory_Lab Complete RecordAsst.prof M.Gokilavani
 
VIP Model Call Girls Kothrud ( Pune ) Call ON 8005736733 Starting From 5K to ...
VIP Model Call Girls Kothrud ( Pune ) Call ON 8005736733 Starting From 5K to ...VIP Model Call Girls Kothrud ( Pune ) Call ON 8005736733 Starting From 5K to ...
VIP Model Call Girls Kothrud ( Pune ) Call ON 8005736733 Starting From 5K to ...SUHANI PANDEY
 
Call Girls Pimpri Chinchwad Call Me 7737669865 Budget Friendly No Advance Boo...
Call Girls Pimpri Chinchwad Call Me 7737669865 Budget Friendly No Advance Boo...Call Girls Pimpri Chinchwad Call Me 7737669865 Budget Friendly No Advance Boo...
Call Girls Pimpri Chinchwad Call Me 7737669865 Budget Friendly No Advance Boo...roncy bisnoi
 
PVC VS. FIBERGLASS (FRP) GRAVITY SEWER - UNI BELL
PVC VS. FIBERGLASS (FRP) GRAVITY SEWER - UNI BELLPVC VS. FIBERGLASS (FRP) GRAVITY SEWER - UNI BELL
PVC VS. FIBERGLASS (FRP) GRAVITY SEWER - UNI BELLManishPatel169454
 
BSides Seattle 2024 - Stopping Ethan Hunt From Taking Your Data.pptx
BSides Seattle 2024 - Stopping Ethan Hunt From Taking Your Data.pptxBSides Seattle 2024 - Stopping Ethan Hunt From Taking Your Data.pptx
BSides Seattle 2024 - Stopping Ethan Hunt From Taking Your Data.pptxfenichawla
 
Call for Papers - International Journal of Intelligent Systems and Applicatio...
Call for Papers - International Journal of Intelligent Systems and Applicatio...Call for Papers - International Journal of Intelligent Systems and Applicatio...
Call for Papers - International Journal of Intelligent Systems and Applicatio...Christo Ananth
 
UNIT-III FMM. DIMENSIONAL ANALYSIS
UNIT-III FMM.        DIMENSIONAL ANALYSISUNIT-III FMM.        DIMENSIONAL ANALYSIS
UNIT-III FMM. DIMENSIONAL ANALYSISrknatarajan
 
Thermal Engineering Unit - I & II . ppt
Thermal Engineering  Unit - I & II . pptThermal Engineering  Unit - I & II . ppt
Thermal Engineering Unit - I & II . pptDineshKumar4165
 
Glass Ceramics: Processing and Properties
Glass Ceramics: Processing and PropertiesGlass Ceramics: Processing and Properties
Glass Ceramics: Processing and PropertiesPrabhanshu Chaturvedi
 
Generative AI or GenAI technology based PPT
Generative AI or GenAI technology based PPTGenerative AI or GenAI technology based PPT
Generative AI or GenAI technology based PPTbhaskargani46
 
Vivazz, Mieres Social Housing Design Spain
Vivazz, Mieres Social Housing Design SpainVivazz, Mieres Social Housing Design Spain
Vivazz, Mieres Social Housing Design Spaintimesproduction05
 
Thermal Engineering-R & A / C - unit - V
Thermal Engineering-R & A / C - unit - VThermal Engineering-R & A / C - unit - V
Thermal Engineering-R & A / C - unit - VDineshKumar4165
 
Call for Papers - Educational Administration: Theory and Practice, E-ISSN: 21...
Call for Papers - Educational Administration: Theory and Practice, E-ISSN: 21...Call for Papers - Educational Administration: Theory and Practice, E-ISSN: 21...
Call for Papers - Educational Administration: Theory and Practice, E-ISSN: 21...Christo Ananth
 
AKTU Computer Networks notes --- Unit 3.pdf
AKTU Computer Networks notes ---  Unit 3.pdfAKTU Computer Networks notes ---  Unit 3.pdf
AKTU Computer Networks notes --- Unit 3.pdfankushspencer015
 

Dernier (20)

The Most Attractive Pune Call Girls Manchar 8250192130 Will You Miss This Cha...
The Most Attractive Pune Call Girls Manchar 8250192130 Will You Miss This Cha...The Most Attractive Pune Call Girls Manchar 8250192130 Will You Miss This Cha...
The Most Attractive Pune Call Girls Manchar 8250192130 Will You Miss This Cha...
 
UNIT-IFLUID PROPERTIES & FLOW CHARACTERISTICS
UNIT-IFLUID PROPERTIES & FLOW CHARACTERISTICSUNIT-IFLUID PROPERTIES & FLOW CHARACTERISTICS
UNIT-IFLUID PROPERTIES & FLOW CHARACTERISTICS
 
CCS335 _ Neural Networks and Deep Learning Laboratory_Lab Complete Record
CCS335 _ Neural Networks and Deep Learning Laboratory_Lab Complete RecordCCS335 _ Neural Networks and Deep Learning Laboratory_Lab Complete Record
CCS335 _ Neural Networks and Deep Learning Laboratory_Lab Complete Record
 
VIP Model Call Girls Kothrud ( Pune ) Call ON 8005736733 Starting From 5K to ...
VIP Model Call Girls Kothrud ( Pune ) Call ON 8005736733 Starting From 5K to ...VIP Model Call Girls Kothrud ( Pune ) Call ON 8005736733 Starting From 5K to ...
VIP Model Call Girls Kothrud ( Pune ) Call ON 8005736733 Starting From 5K to ...
 
Call Girls Pimpri Chinchwad Call Me 7737669865 Budget Friendly No Advance Boo...
Call Girls Pimpri Chinchwad Call Me 7737669865 Budget Friendly No Advance Boo...Call Girls Pimpri Chinchwad Call Me 7737669865 Budget Friendly No Advance Boo...
Call Girls Pimpri Chinchwad Call Me 7737669865 Budget Friendly No Advance Boo...
 
PVC VS. FIBERGLASS (FRP) GRAVITY SEWER - UNI BELL
PVC VS. FIBERGLASS (FRP) GRAVITY SEWER - UNI BELLPVC VS. FIBERGLASS (FRP) GRAVITY SEWER - UNI BELL
PVC VS. FIBERGLASS (FRP) GRAVITY SEWER - UNI BELL
 
Roadmap to Membership of RICS - Pathways and Routes
Roadmap to Membership of RICS - Pathways and RoutesRoadmap to Membership of RICS - Pathways and Routes
Roadmap to Membership of RICS - Pathways and Routes
 
BSides Seattle 2024 - Stopping Ethan Hunt From Taking Your Data.pptx
BSides Seattle 2024 - Stopping Ethan Hunt From Taking Your Data.pptxBSides Seattle 2024 - Stopping Ethan Hunt From Taking Your Data.pptx
BSides Seattle 2024 - Stopping Ethan Hunt From Taking Your Data.pptx
 
Call for Papers - International Journal of Intelligent Systems and Applicatio...
Call for Papers - International Journal of Intelligent Systems and Applicatio...Call for Papers - International Journal of Intelligent Systems and Applicatio...
Call for Papers - International Journal of Intelligent Systems and Applicatio...
 
(INDIRA) Call Girl Aurangabad Call Now 8617697112 Aurangabad Escorts 24x7
(INDIRA) Call Girl Aurangabad Call Now 8617697112 Aurangabad Escorts 24x7(INDIRA) Call Girl Aurangabad Call Now 8617697112 Aurangabad Escorts 24x7
(INDIRA) Call Girl Aurangabad Call Now 8617697112 Aurangabad Escorts 24x7
 
UNIT-III FMM. DIMENSIONAL ANALYSIS
UNIT-III FMM.        DIMENSIONAL ANALYSISUNIT-III FMM.        DIMENSIONAL ANALYSIS
UNIT-III FMM. DIMENSIONAL ANALYSIS
 
(INDIRA) Call Girl Bhosari Call Now 8617697112 Bhosari Escorts 24x7
(INDIRA) Call Girl Bhosari Call Now 8617697112 Bhosari Escorts 24x7(INDIRA) Call Girl Bhosari Call Now 8617697112 Bhosari Escorts 24x7
(INDIRA) Call Girl Bhosari Call Now 8617697112 Bhosari Escorts 24x7
 
Thermal Engineering Unit - I & II . ppt
Thermal Engineering  Unit - I & II . pptThermal Engineering  Unit - I & II . ppt
Thermal Engineering Unit - I & II . ppt
 
NFPA 5000 2024 standard .
NFPA 5000 2024 standard                                  .NFPA 5000 2024 standard                                  .
NFPA 5000 2024 standard .
 
Glass Ceramics: Processing and Properties
Glass Ceramics: Processing and PropertiesGlass Ceramics: Processing and Properties
Glass Ceramics: Processing and Properties
 
Generative AI or GenAI technology based PPT
Generative AI or GenAI technology based PPTGenerative AI or GenAI technology based PPT
Generative AI or GenAI technology based PPT
 
Vivazz, Mieres Social Housing Design Spain
Vivazz, Mieres Social Housing Design SpainVivazz, Mieres Social Housing Design Spain
Vivazz, Mieres Social Housing Design Spain
 
Thermal Engineering-R & A / C - unit - V
Thermal Engineering-R & A / C - unit - VThermal Engineering-R & A / C - unit - V
Thermal Engineering-R & A / C - unit - V
 
Call for Papers - Educational Administration: Theory and Practice, E-ISSN: 21...
Call for Papers - Educational Administration: Theory and Practice, E-ISSN: 21...Call for Papers - Educational Administration: Theory and Practice, E-ISSN: 21...
Call for Papers - Educational Administration: Theory and Practice, E-ISSN: 21...
 
AKTU Computer Networks notes --- Unit 3.pdf
AKTU Computer Networks notes ---  Unit 3.pdfAKTU Computer Networks notes ---  Unit 3.pdf
AKTU Computer Networks notes --- Unit 3.pdf
 

RxJava on Android

  • 2. Declarative vs. Imperative Programming ➤ Imperative Programming: telling the machine how to do something, and as a result what you want to happen will happen. ➤ Declarative Programming: telling the machine what you would like to happen, and let the computer figure out how to do it. var numbers = [1,2,3,4,5] var doubled = [] for(var i = 0; i < numbers.length; i++) { var newNumber = numbers[i] * 2 doubled.push(newNumber) } console.log(doubled) //=> [2,4,6,8,10] var numbers = [1,2,3,4,5] var total = 0 for(var i = 0; i < numbers.length; i++) { total += numbers[i] } console.log(total) //=> 15 var numbers = [1,2,3,4,5] var doubled = numbers.map(function(n) { return n * 2 }) console.log(doubled) //=> [2,4,6,8,10] var numbers = [1,2,3,4,5] var total = numbers.reduce(function(sum, n) { return sum + n }); console.log(total) //=> 15
  • 3. Think about spreadsheets A cell does not represent a value, but a potential stream of values.
 That stream can be split into many streams, combined or fed into other streams. There are high-order functions that can consume these streams:
 ‘AVERAGE’, ‘SUM’,’SUMIF’,’POWER’, etc… Functional Reactive Programming Savings Percent 0.2 Monthly Wages 1600 Hourly Wage 10 Annual Wages 19200 Hours Worked/Wk 40 Annual Savings 3840 Weekly Wages 400 Savings Percent 0.4 Monthly Wages 3200 Hourly Wage 20 Annual Wages 38400 Hours Worked/Wk 40 Annual Savings 15360 Weekly Wages 800
  • 4. What is RxJava? ➤ Observables & Subscribers ➤ High-order functions ➤ Schedulers
  • 5. Observer Contract Observable.just(1,2,3,4,5) .subscribe(new Subscriber<Integer>() { @Override public void onCompleted() { Log.d("Test", "sequence completed"); } @Override public void onError(Throwable e) { Log.e("Test", "got error: " + e.getMessage()); } @Override public void onNext(Integer integer) { Log.d("Test", "got int: " + integer); } }); The contract is very simple: onNext,onError,onCompleted
  • 6. High-order functions ➤ filter ➤ map ➤ cast ➤ buffer ➤ debounce ➤ merge ➤ zip ➤ combineLatest ➤ flatMap A few common examples:
  • 8. Map
  • 12. Merge
  • 13. Zip
  • 16. Schedulers ➤ computation - for computation work ➤ immediate - current thread ➤ io - IO-bound work ➤ newThread - new thread for each unit ➤ test - useful for debugging ➤ trampoline - current thread, after current work ➤ AndroidSchedulers.mainThread (RxAndroid) Consistent with the purpose of the high-order functions, RxJava’s schedulers remove the concern of how code gets run on different threads and allows the developer to simply declare which scheduler should run which units of work.
  • 17. Example Scheduler Usage Observable .just(1) .delay(10, TimeUnit.SECONDS, Schedulers.io()) .observeOn(AndroidSchedulers.mainThread()) .subscribe(new Subscriber<Integer>() { @Override public void onCompleted() { } @Override public void onError(Throwable e) { } @Override public void onNext(Integer integer) { Log.d("Test", "this is on the main thread!"); } });
  • 18. RxJava + Schedulers vs AsyncTask ➤ Easier to use ➤ More effective error handling (built-in) ➤ Use of high-order functions ➤ Easily cancelable ➤ Easily retry-able ➤ Testable ➤ Greater control over concurrency ➤ AsyncTask has just 2 options, ThreadPoolExecutor or not and you can’t control what thread the onPostExecute fires on. representativeApi.representativesByZipCode(zipCode) .subscribeOn(Schedulers.io()) .observeOn(AndroidSchedulers.mainThread()) .toList() .retry(2) .subscribe(onRepresentativesReceived()); A compelling RxJava Example
  • 19. RxJava on Android ➤ RxAndroid - minimum base ➤ RxLifecycle - easy lifecycle handling ➤ RxBinding - easily create observables from Android UI widgets As of this writing, RxAndroid has had a lot of recent changes. Formerly, it contained nearly every useful thing required to build RxJava apps in Android. It has now been broken up into several constituent pieces.
  • 20. RxLifecycle @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); Observable .interval(1, TimeUnit.SECONDS, Schedulers.io()) .compose(this.<Long>bindUntilEvent(ActivityEvent.DESTROY)) .observeOn(AndroidSchedulers.mainThread()) .subscribe(new Action1<Long>() { @Override public void call(Long aLong) { Log.d("Test", "logging stuff until destroyed"); } }); } Makes it easy to keep updates happening within safe timeframes
  • 21. RxBinding Observable<Double> hourlyWageObservable = RxTextView.textChanges(_hourlyWage) .map(new Func1<CharSequence, Double>() { @Override public Double call(CharSequence charSequence) { try { return Double.parseDouble(charSequence.toString()); } catch (NumberFormatException e) { return 0D; } } }); Makes it easy to create Observables from Android UI widgets
  • 22. RxJava Challenges ➤ No lambdas on Android
 (requires Java 8) ➤ Lots of extra code, but nearly negated by solid auto-complete tools ➤ Can be overcome with retrolambda but it can be fickle ➤ Steep learning curve ➤ Easy traditional solutions can seem difficult to perform with RxJava at first
  • 23. Useful compatible libraries ➤ SqlBrite ➤ Retrofit - example ➤ RxPreferences
  • 24. Testing ➤ One Interesting Article ➤ Another Interesting Article
  • 25. Sample Project ➤ RxAndroid-Sample ➤ Use of RxAndroid, RxLifecycle, RxBinding ➤ Use of Retrofit with RxJava ➤ Example of converting non- RxJava code to work with RxJava ➤ More…
  • 26. Final Words RxJava is not about writing less code or doing things that were impossible before. It’s about focusing more lines of code toward your application’s actual objectives. It helps with this by removing all the tedious, generic coding that we have to do every day: allowing the solved problems stay solved while we move on to more important things. When we can reduce the number of wrote, for-loop collection manipulation routines we can do in our sleep we can spend more time thinking about and solving the real problems.