SlideShare a Scribd company logo
1 of 25
Download to read offline
RXJAVA2
TIPS AND TRICKS
@STEPANGO
ABOUT ME
▸ Android developer since 2008
▸ Lead Android Engineer @ 90Seconds.tv
▸ Kotlin and Rx addict
▸ RxKotlin contributor
▸ RxDataBindings author
Event Iterable Observable
retrieve data T next() onNext(T)
discover error throw Exception onError(Exception)
complete !hasNext() onCompleted()
transform fromIterbale() toList()
SHORTEST RX INTRO EVER
1.0 VS 2.0
▸ Mar 2018 - end of rxJava1 support
▸ Better performance
▸ Lower memory consumption
▸ Can’t use null
▸ Reactive-Streams based
▸ Maybe, Flowable
OBSERVABLE AND FRIENDS
▸ Flowable: 0..N flows, supporting Reactive-Streams and
backpressure
▸ Observable: 0..N flows, no backpressure
▸ Single: a flow of exactly 1 item or an error
▸ Completable: a flow without items but only a completion
or error signal
▸ Maybe: a flow with no items, exactly one item or an error
HOW TO DEAL WITH NULLS
▸ Null object pattern
▸ Optional
▸ State wrappers
NULL OBJECT PATTERN
interface Animal {
void makeSound();
}
class Dog implements Animal {
public void makeSound() {
System.out.println("woof!");
}
}
enum NullAnimal implements Animal {
INSTANCE;
public void makeSound() {};
}
void execute() {
List<Animal> animals = Arrays.asList(NullAnimal.INSTANCE, new Dog());
Observable.fromIterable(animals)
.filter(animal J> animal != NullAnimal.INSTANCE)
.subscribe(AnimalKLmakeSound);
}
STREAM OF OPTIONAL VALUES
interface Animal {
void makeSound();
}
class Dog implements Animal {
public void makeSound() {
System.out.println("woof!");
}
}
public void execute() {
List<Optional<Animal>> animals = Arrays.asList(
Optional.<Animal>empty(), Optional.of(new Dog())
);
Observable.fromIterable(animals)
.filter(OptionalKLisPresent)
.map(OptionalKLget)
.subscribe(AnimalKLmakeSound);
}
STREAM OF STATES
class AnimalStateSuccess implements AnimalState {
Animal animal;
AnimalStateSuccess(Animal animal) { this.animal = animal; }
@Override public boolean hasAnimal() { return true;}
@Override public Animal animal() { return animal; }
@Override public String errorMsg() { return null; }
}
enum AnimalStateError implements AnimalState {
INSTANCE;
@Override public boolean hasAnimal() { return false;}
@Override public Animal animal() { return null; }
@Override public String errorMsg() { return "We lost him"; }
}
public void execute() {
List<AnimalState> animals = Arrays.asList(
AnimalStateError.INSTANCE, new AnimalStateSuccess(new Dog())
);
Observable.fromIterable(animals)
.filter(AnimalStateKLhasAnimal)
.map(AnimalStateKLanimal)
.subscribe(AnimalKLmakeSound);
}
TESTING IS EASY
▸ Blocking calls
▸ TestSubscriber TestObserver
▸ RxJavaPlugins
▸ await()/awaitTerminalEvent()
▸ TestScheduler
BLOCKING CALLS
Observable<Integer> observable = Observable.just(1, 2);
int first = observable.blockingFirst();
assertEquals(first, 1);
int last = observable.blockingLast();
assertEquals(last, 2);
Iterable<Integer> integers = observable.blockingIterable();
int sum = 0;
for (Integer integer : integers) sum += integer;
assertEquals(sum, 3);
TEST SUBSCRIBER
Observable.just(1, 2)
.test()
.assertNoErrors()
.assertComplete()
.assertResult(1, 2)
.assertNever(3);
TEST SUBSCRIBER
Observable.timer(1, TimeUnit.SECONDS)
.flatMap(ignore J> Observable.just(1, 2))
.test()
.assertNoErrors()
.assertComplete()
.assertResult(1, 2)
.assertNever(3);
TEST SUBSCRIBER
Observable.timer(1, TimeUnit.SECONDS)
.flatMap(ignore J> Observable.just(1, 2))
.test()
.assertNoErrors()
.assertComplete()
.assertResult(1, 2)
.assertNever(3);
FAIL
TEST SUBSCRIBER
Observable.timer(1, TimeUnit.SECONDS)
.flatMap(ignore J> Observable.just(1, 2))
.test()
.await()
.assertNoErrors()
.assertComplete()
.assertResult(1, 2)
.assertNever(3);
AWAIT TERMINAL EVENT
boolean hasTerminalEvent = Observable.timer(1, TimeUnit.SECONDS)
.flatMap(ignore J> Observable.just(1, 2))
.test()
.awaitTerminalEvent(500, TimeUnit.MILLISECONDS);
assertFalse(hasTerminalEvent);
PREVENT FROM FAILING
Observable.timer(1, TimeUnit.SECONDS)
.flatMap(ignore J> Observable.just(1, 2))
.test()
.assertNoErrors()
.assertComplete()
.assertResult(1, 2)
.assertNever(3);
UNIT TEST RULE
@Rule
final TrampolineSchedulersRule schedulers = new
TrampolineSchedulersRule();
@Test
public void observerTest() throws InterruptedException {
Observable.timer(1, TimeUnit.SECONDS)
.flatMap(ignore J> Observable.just(1, 2))
.test()
.assertNoErrors()
.assertComplete()
.assertResult(1, 2)
.assertNever(3);
}
TEST SUBSCRIBER
public class TrampolineSchedulersRule implements TestRule {
@Override
public Statement apply(final Statement base, Description description) {
return new Statement() {
@Override public void evaluate() throws Throwable {
RxJavaPlugins.setIoSchedulerHandler(
scheduler J> Schedulers.trampoline());
RxJavaPlugins.setComputationSchedulerHandler(
scheduler J> Schedulers.trampoline());
RxJavaPlugins.setNewThreadSchedulerHandler(
scheduler J> Schedulers.trampoline());
try {
base.evaluate();
} finally {
RxJavaPlugins.reset();
}
}
};
}
}
TEST SUBSCRIBER
public class TrampolineSchedulersRule implements TestRule {
@Override
public Statement apply(final Statement base, Description description) {
return new Statement() {
@Override public void evaluate() throws Throwable {
RxJavaPlugins.setIoSchedulerHandler(
scheduler J> Schedulers.trampoline());
RxJavaPlugins.setComputationSchedulerHandler(
scheduler J> Schedulers.trampoline());
RxJavaPlugins.setNewThreadSchedulerHandler(
scheduler J> Schedulers.trampoline());
try {
base.evaluate();
} finally {
RxJavaPlugins.reset();
}
}
};
}
}
TIME CONTROL
Observable<Integer> externalObservable = Observable.timer(10, TimeUnit.SECONDS)
.flatMap(ignore J> Observable.just(1, 2));
externalObservable.test()
.await()
.assertNoErrors()
.assertComplete()
.assertResult(1, 2)
.assertNever(3);
TIME CONTROL
@Test public void subscriberTest() throws InterruptedException {
Observable<Integer> externalObservable = Observable.timer(10, TimeUnit.SECONDS)
.flatMap(ignore J> Observable.just(1, 2));
TestObserver<Integer> testObserver = externalObservable.test();
testObserver.assertNoValues();
testObserver.await()
.assertNoErrors()
.assertComplete()
.assertResult(1, 2)
.assertNever(3);
}
TIME CONTROL
@Rule
public TestSchedulersRule testSchedulerRule = new TestSchedulersRule();
private TestScheduler testScheduler = testSchedulerRule.testScheduler;
@Test public void subscriberTest() throws InterruptedException {
Observable<Integer> externalObservable = Observable.timer(10, TimeUnit.SECONDS)
.flatMap(ignore J> Observable.just(1, 2));
TestObserver<Integer> testObserver = externalObservable.test();
testObserver.assertNoValues();
testScheduler.advanceTimeBy(10, TimeUnit.SECONDS);
testObserver
.assertNoErrors()
.assertComplete()
.assertResult(1, 2)
.assertNever(3);
}
TEXT
public class TestSchedulersRule implements TestRule {
TestScheduler testScheduler = new TestScheduler();
@Override
public Statement apply(final Statement base, Description description) {
return new Statement() {
@Override public void evaluate() throws Throwable {
RxJavaPlugins.setIoSchedulerHandler(
scheduler J> testScheduler);
RxJavaPlugins.setComputationSchedulerHandler(
scheduler J> testScheduler);
RxJavaPlugins.setNewThreadSchedulerHandler(
scheduler J> testScheduler);
try {
base.evaluate();
} finally {
RxJavaPlugins.reset();
}
}
};
}
}
THANKS
@STEPANGO
90seconds.tv

More Related Content

What's hot

Rx java testing patterns
Rx java testing patternsRx java testing patterns
Rx java testing patterns彥彬 洪
 
Advanced patterns in asynchronous programming
Advanced patterns in asynchronous programmingAdvanced patterns in asynchronous programming
Advanced patterns in asynchronous programmingMichael Arenzon
 
Fine grain process control 2nd nov
Fine grain process control 2nd novFine grain process control 2nd nov
Fine grain process control 2nd novSurendraGangarapu1
 
OTcl and C++ linkages in NS2
OTcl and C++ linkages in NS2OTcl and C++ linkages in NS2
OTcl and C++ linkages in NS2Pradeep Kumar TS
 
Async Testing giving you a sinking feeling
Async Testing giving you a sinking feelingAsync Testing giving you a sinking feeling
Async Testing giving you a sinking feelingErin Zimmer
 
Dagger & rxjava & retrofit
Dagger & rxjava & retrofitDagger & rxjava & retrofit
Dagger & rxjava & retrofitTed Liang
 
Registro de venta
Registro de ventaRegistro de venta
Registro de ventalupe ga
 
Cascadia.js: Don't Cross the Streams
Cascadia.js: Don't Cross the StreamsCascadia.js: Don't Cross the Streams
Cascadia.js: Don't Cross the Streamsmattpodwysocki
 
並行処理プログラミングの深淵~Java仮想マシン仕様 スレッドとロック~
並行処理プログラミングの深淵~Java仮想マシン仕様 スレッドとロック~並行処理プログラミングの深淵~Java仮想マシン仕様 スレッドとロック~
並行処理プログラミングの深淵~Java仮想マシン仕様 スレッドとロック~Kazuhiro Eguchi
 
Functional Reactive Programming / Compositional Event Systems
Functional Reactive Programming / Compositional Event SystemsFunctional Reactive Programming / Compositional Event Systems
Functional Reactive Programming / Compositional Event SystemsLeonardo Borges
 
Preparation for mit ose lab4
Preparation for mit ose lab4Preparation for mit ose lab4
Preparation for mit ose lab4Benux Wei
 
Think Async: Asynchronous Patterns in NodeJS
Think Async: Asynchronous Patterns in NodeJSThink Async: Asynchronous Patterns in NodeJS
Think Async: Asynchronous Patterns in NodeJSAdam L Barrett
 
Introduction to Unit Testing (Part 2 of 2)
Introduction to Unit Testing (Part 2 of 2)Introduction to Unit Testing (Part 2 of 2)
Introduction to Unit Testing (Part 2 of 2)Dennis Byrne
 
Async JavaScript in ES7
Async JavaScript in ES7Async JavaScript in ES7
Async JavaScript in ES7Mike North
 
Fighting async JavaScript (CSP)
Fighting async JavaScript (CSP)Fighting async JavaScript (CSP)
Fighting async JavaScript (CSP)Max Klymyshyn
 

What's hot (20)

Rxjava meetup presentation
Rxjava meetup presentationRxjava meetup presentation
Rxjava meetup presentation
 
Rx java testing patterns
Rx java testing patternsRx java testing patterns
Rx java testing patterns
 
Advanced patterns in asynchronous programming
Advanced patterns in asynchronous programmingAdvanced patterns in asynchronous programming
Advanced patterns in asynchronous programming
 
Fine grain process control 2nd nov
Fine grain process control 2nd novFine grain process control 2nd nov
Fine grain process control 2nd nov
 
Iniciación rx java
Iniciación rx javaIniciación rx java
Iniciación rx java
 
Good Tests Bad Tests
Good Tests Bad TestsGood Tests Bad Tests
Good Tests Bad Tests
 
OTcl and C++ linkages in NS2
OTcl and C++ linkages in NS2OTcl and C++ linkages in NS2
OTcl and C++ linkages in NS2
 
Async Testing giving you a sinking feeling
Async Testing giving you a sinking feelingAsync Testing giving you a sinking feeling
Async Testing giving you a sinking feeling
 
Dagger & rxjava & retrofit
Dagger & rxjava & retrofitDagger & rxjava & retrofit
Dagger & rxjava & retrofit
 
Registro de venta
Registro de ventaRegistro de venta
Registro de venta
 
Cascadia.js: Don't Cross the Streams
Cascadia.js: Don't Cross the StreamsCascadia.js: Don't Cross the Streams
Cascadia.js: Don't Cross the Streams
 
並行処理プログラミングの深淵~Java仮想マシン仕様 スレッドとロック~
並行処理プログラミングの深淵~Java仮想マシン仕様 スレッドとロック~並行処理プログラミングの深淵~Java仮想マシン仕様 スレッドとロック~
並行処理プログラミングの深淵~Java仮想マシン仕様 スレッドとロック~
 
Functional Reactive Programming / Compositional Event Systems
Functional Reactive Programming / Compositional Event SystemsFunctional Reactive Programming / Compositional Event Systems
Functional Reactive Programming / Compositional Event Systems
 
devday2012
devday2012devday2012
devday2012
 
Preparation for mit ose lab4
Preparation for mit ose lab4Preparation for mit ose lab4
Preparation for mit ose lab4
 
Android TDD & CI
Android TDD & CIAndroid TDD & CI
Android TDD & CI
 
Think Async: Asynchronous Patterns in NodeJS
Think Async: Asynchronous Patterns in NodeJSThink Async: Asynchronous Patterns in NodeJS
Think Async: Asynchronous Patterns in NodeJS
 
Introduction to Unit Testing (Part 2 of 2)
Introduction to Unit Testing (Part 2 of 2)Introduction to Unit Testing (Part 2 of 2)
Introduction to Unit Testing (Part 2 of 2)
 
Async JavaScript in ES7
Async JavaScript in ES7Async JavaScript in ES7
Async JavaScript in ES7
 
Fighting async JavaScript (CSP)
Fighting async JavaScript (CSP)Fighting async JavaScript (CSP)
Fighting async JavaScript (CSP)
 

Similar to rxJava 2 tips and tricks

Rx java2 - Should I use it?
Rx java2 - Should I use it?Rx java2 - Should I use it?
Rx java2 - Should I use it?Kamil Kucharski
 
Akka.NET streams and reactive streams
Akka.NET streams and reactive streamsAkka.NET streams and reactive streams
Akka.NET streams and reactive streamsBartosz Sypytkowski
 
Reactive Programming on Android
Reactive Programming on AndroidReactive Programming on Android
Reactive Programming on AndroidGuilherme Branco
 
A Playful Introduction to Rx
A Playful Introduction to RxA Playful Introduction to Rx
A Playful Introduction to RxAndrey Cheptsov
 
Understanding reactive programming with microsoft reactive extensions
Understanding reactive programming  with microsoft reactive extensionsUnderstanding reactive programming  with microsoft reactive extensions
Understanding reactive programming with microsoft reactive extensionsOleksandr Zhevzhyk
 
RxJava и Android. Плюсы, минусы, подводные камни
RxJava и Android. Плюсы, минусы, подводные камниRxJava и Android. Плюсы, минусы, подводные камни
RxJava и Android. Плюсы, минусы, подводные камниStfalcon Meetups
 
Test-driven Development no Rails
Test-driven Development no RailsTest-driven Development no Rails
Test-driven Development no Railselliando dias
 
Animation in Java
Animation in JavaAnimation in Java
Animation in JavaAlan Goo
 
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 JVMNetesh Kumar
 
Reactive Programming with RxSwift
Reactive Programming with RxSwiftReactive Programming with RxSwift
Reactive Programming with RxSwiftScott Gardner
 
Test-driven Development no Rails - Começando com o pé direito
Test-driven Development no Rails - Começando com o pé direitoTest-driven Development no Rails - Começando com o pé direito
Test-driven Development no Rails - Começando com o pé direitoNando Vieira
 
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 ReactingIndicThreads
 
TLA+ and PlusCal / An engineer's perspective
TLA+ and PlusCal / An engineer's perspectiveTLA+ and PlusCal / An engineer's perspective
TLA+ and PlusCal / An engineer's perspectiveTorao Takami
 

Similar to rxJava 2 tips and tricks (20)

Rx java2 - Should I use it?
Rx java2 - Should I use it?Rx java2 - Should I use it?
Rx java2 - Should I use it?
 
Akka.NET streams and reactive streams
Akka.NET streams and reactive streamsAkka.NET streams and reactive streams
Akka.NET streams and reactive streams
 
Rx workshop
Rx workshopRx workshop
Rx workshop
 
Taming Asynchrony using RxJS
Taming Asynchrony using RxJSTaming Asynchrony using RxJS
Taming Asynchrony using RxJS
 
Reactive Programming on Android
Reactive Programming on AndroidReactive Programming on Android
Reactive Programming on Android
 
A Playful Introduction to Rx
A Playful Introduction to RxA Playful Introduction to Rx
A Playful Introduction to Rx
 
2 презентация rx java+android
2 презентация rx java+android2 презентация rx java+android
2 презентация rx java+android
 
Understanding reactive programming with microsoft reactive extensions
Understanding reactive programming  with microsoft reactive extensionsUnderstanding reactive programming  with microsoft reactive extensions
Understanding reactive programming with microsoft reactive extensions
 
RxJava on Android
RxJava on AndroidRxJava on Android
RxJava on Android
 
RxJava и Android. Плюсы, минусы, подводные камни
RxJava и Android. Плюсы, минусы, подводные камниRxJava и Android. Плюсы, минусы, подводные камни
RxJava и Android. Плюсы, минусы, подводные камни
 
Saving lives with rx java
Saving lives with rx javaSaving lives with rx java
Saving lives with rx java
 
Test-driven Development no Rails
Test-driven Development no RailsTest-driven Development no Rails
Test-driven Development no Rails
 
Animation in Java
Animation in JavaAnimation in Java
Animation in Java
 
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
 
Clojure functions midje
Clojure functions midjeClojure functions midje
Clojure functions midje
 
Reactive Programming with RxSwift
Reactive Programming with RxSwiftReactive Programming with RxSwift
Reactive Programming with RxSwift
 
Test-driven Development no Rails - Começando com o pé direito
Test-driven Development no Rails - Começando com o pé direitoTest-driven Development no Rails - Começando com o pé direito
Test-driven Development no Rails - Começando com o pé direito
 
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
 
TLA+ and PlusCal / An engineer's perspective
TLA+ and PlusCal / An engineer's perspectiveTLA+ and PlusCal / An engineer's perspective
TLA+ and PlusCal / An engineer's perspective
 
Reactive x
Reactive xReactive x
Reactive x
 

Recently uploaded

US Department of Education FAFSA Week of Action
US Department of Education FAFSA Week of ActionUS Department of Education FAFSA Week of Action
US Department of Education FAFSA Week of ActionMebane Rash
 
An experimental study in using natural admixture as an alternative for chemic...
An experimental study in using natural admixture as an alternative for chemic...An experimental study in using natural admixture as an alternative for chemic...
An experimental study in using natural admixture as an alternative for chemic...Chandu841456
 
Software and Systems Engineering Standards: Verification and Validation of Sy...
Software and Systems Engineering Standards: Verification and Validation of Sy...Software and Systems Engineering Standards: Verification and Validation of Sy...
Software and Systems Engineering Standards: Verification and Validation of Sy...VICTOR MAESTRE RAMIREZ
 
Application of Residue Theorem to evaluate real integrations.pptx
Application of Residue Theorem to evaluate real integrations.pptxApplication of Residue Theorem to evaluate real integrations.pptx
Application of Residue Theorem to evaluate real integrations.pptx959SahilShah
 
welding defects observed during the welding
welding defects observed during the weldingwelding defects observed during the welding
welding defects observed during the weldingMuhammadUzairLiaqat
 
Oxy acetylene welding presentation note.
Oxy acetylene welding presentation note.Oxy acetylene welding presentation note.
Oxy acetylene welding presentation note.eptoze12
 
8251 universal synchronous asynchronous receiver transmitter
8251 universal synchronous asynchronous receiver transmitter8251 universal synchronous asynchronous receiver transmitter
8251 universal synchronous asynchronous receiver transmitterShivangiSharma879191
 
CCS355 Neural Networks & Deep Learning Unit 1 PDF notes with Question bank .pdf
CCS355 Neural Networks & Deep Learning Unit 1 PDF notes with Question bank .pdfCCS355 Neural Networks & Deep Learning Unit 1 PDF notes with Question bank .pdf
CCS355 Neural Networks & Deep Learning Unit 1 PDF notes with Question bank .pdfAsst.prof M.Gokilavani
 
Electronically Controlled suspensions system .pdf
Electronically Controlled suspensions system .pdfElectronically Controlled suspensions system .pdf
Electronically Controlled suspensions system .pdfme23b1001
 
Why does (not) Kafka need fsync: Eliminating tail latency spikes caused by fsync
Why does (not) Kafka need fsync: Eliminating tail latency spikes caused by fsyncWhy does (not) Kafka need fsync: Eliminating tail latency spikes caused by fsync
Why does (not) Kafka need fsync: Eliminating tail latency spikes caused by fsyncssuser2ae721
 
Call Girls Narol 7397865700 Independent Call Girls
Call Girls Narol 7397865700 Independent Call GirlsCall Girls Narol 7397865700 Independent Call Girls
Call Girls Narol 7397865700 Independent Call Girlsssuser7cb4ff
 
Unit7-DC_Motors nkkjnsdkfnfcdfknfdgfggfg
Unit7-DC_Motors nkkjnsdkfnfcdfknfdgfggfgUnit7-DC_Motors nkkjnsdkfnfcdfknfdgfggfg
Unit7-DC_Motors nkkjnsdkfnfcdfknfdgfggfgsaravananr517913
 
Vishratwadi & Ghorpadi Bridge Tender documents
Vishratwadi & Ghorpadi Bridge Tender documentsVishratwadi & Ghorpadi Bridge Tender documents
Vishratwadi & Ghorpadi Bridge Tender documentsSachinPawar510423
 
Sachpazis Costas: Geotechnical Engineering: A student's Perspective Introduction
Sachpazis Costas: Geotechnical Engineering: A student's Perspective IntroductionSachpazis Costas: Geotechnical Engineering: A student's Perspective Introduction
Sachpazis Costas: Geotechnical Engineering: A student's Perspective IntroductionDr.Costas Sachpazis
 
INFLUENCE OF NANOSILICA ON THE PROPERTIES OF CONCRETE
INFLUENCE OF NANOSILICA ON THE PROPERTIES OF CONCRETEINFLUENCE OF NANOSILICA ON THE PROPERTIES OF CONCRETE
INFLUENCE OF NANOSILICA ON THE PROPERTIES OF CONCRETEroselinkalist12
 
UNIT III ANALOG ELECTRONICS (BASIC ELECTRONICS)
UNIT III ANALOG ELECTRONICS (BASIC ELECTRONICS)UNIT III ANALOG ELECTRONICS (BASIC ELECTRONICS)
UNIT III ANALOG ELECTRONICS (BASIC ELECTRONICS)Dr SOUNDIRARAJ N
 
CCS355 Neural Network & Deep Learning UNIT III notes and Question bank .pdf
CCS355 Neural Network & Deep Learning UNIT III notes and Question bank .pdfCCS355 Neural Network & Deep Learning UNIT III notes and Question bank .pdf
CCS355 Neural Network & Deep Learning UNIT III notes and Question bank .pdfAsst.prof M.Gokilavani
 

Recently uploaded (20)

US Department of Education FAFSA Week of Action
US Department of Education FAFSA Week of ActionUS Department of Education FAFSA Week of Action
US Department of Education FAFSA Week of Action
 
An experimental study in using natural admixture as an alternative for chemic...
An experimental study in using natural admixture as an alternative for chemic...An experimental study in using natural admixture as an alternative for chemic...
An experimental study in using natural admixture as an alternative for chemic...
 
Design and analysis of solar grass cutter.pdf
Design and analysis of solar grass cutter.pdfDesign and analysis of solar grass cutter.pdf
Design and analysis of solar grass cutter.pdf
 
9953056974 Call Girls In South Ex, Escorts (Delhi) NCR.pdf
9953056974 Call Girls In South Ex, Escorts (Delhi) NCR.pdf9953056974 Call Girls In South Ex, Escorts (Delhi) NCR.pdf
9953056974 Call Girls In South Ex, Escorts (Delhi) NCR.pdf
 
Software and Systems Engineering Standards: Verification and Validation of Sy...
Software and Systems Engineering Standards: Verification and Validation of Sy...Software and Systems Engineering Standards: Verification and Validation of Sy...
Software and Systems Engineering Standards: Verification and Validation of Sy...
 
Application of Residue Theorem to evaluate real integrations.pptx
Application of Residue Theorem to evaluate real integrations.pptxApplication of Residue Theorem to evaluate real integrations.pptx
Application of Residue Theorem to evaluate real integrations.pptx
 
welding defects observed during the welding
welding defects observed during the weldingwelding defects observed during the welding
welding defects observed during the welding
 
Oxy acetylene welding presentation note.
Oxy acetylene welding presentation note.Oxy acetylene welding presentation note.
Oxy acetylene welding presentation note.
 
8251 universal synchronous asynchronous receiver transmitter
8251 universal synchronous asynchronous receiver transmitter8251 universal synchronous asynchronous receiver transmitter
8251 universal synchronous asynchronous receiver transmitter
 
CCS355 Neural Networks & Deep Learning Unit 1 PDF notes with Question bank .pdf
CCS355 Neural Networks & Deep Learning Unit 1 PDF notes with Question bank .pdfCCS355 Neural Networks & Deep Learning Unit 1 PDF notes with Question bank .pdf
CCS355 Neural Networks & Deep Learning Unit 1 PDF notes with Question bank .pdf
 
Electronically Controlled suspensions system .pdf
Electronically Controlled suspensions system .pdfElectronically Controlled suspensions system .pdf
Electronically Controlled suspensions system .pdf
 
Why does (not) Kafka need fsync: Eliminating tail latency spikes caused by fsync
Why does (not) Kafka need fsync: Eliminating tail latency spikes caused by fsyncWhy does (not) Kafka need fsync: Eliminating tail latency spikes caused by fsync
Why does (not) Kafka need fsync: Eliminating tail latency spikes caused by fsync
 
Call Girls Narol 7397865700 Independent Call Girls
Call Girls Narol 7397865700 Independent Call GirlsCall Girls Narol 7397865700 Independent Call Girls
Call Girls Narol 7397865700 Independent Call Girls
 
Unit7-DC_Motors nkkjnsdkfnfcdfknfdgfggfg
Unit7-DC_Motors nkkjnsdkfnfcdfknfdgfggfgUnit7-DC_Motors nkkjnsdkfnfcdfknfdgfggfg
Unit7-DC_Motors nkkjnsdkfnfcdfknfdgfggfg
 
Vishratwadi & Ghorpadi Bridge Tender documents
Vishratwadi & Ghorpadi Bridge Tender documentsVishratwadi & Ghorpadi Bridge Tender documents
Vishratwadi & Ghorpadi Bridge Tender documents
 
Sachpazis Costas: Geotechnical Engineering: A student's Perspective Introduction
Sachpazis Costas: Geotechnical Engineering: A student's Perspective IntroductionSachpazis Costas: Geotechnical Engineering: A student's Perspective Introduction
Sachpazis Costas: Geotechnical Engineering: A student's Perspective Introduction
 
INFLUENCE OF NANOSILICA ON THE PROPERTIES OF CONCRETE
INFLUENCE OF NANOSILICA ON THE PROPERTIES OF CONCRETEINFLUENCE OF NANOSILICA ON THE PROPERTIES OF CONCRETE
INFLUENCE OF NANOSILICA ON THE PROPERTIES OF CONCRETE
 
young call girls in Green Park🔝 9953056974 🔝 escort Service
young call girls in Green Park🔝 9953056974 🔝 escort Serviceyoung call girls in Green Park🔝 9953056974 🔝 escort Service
young call girls in Green Park🔝 9953056974 🔝 escort Service
 
UNIT III ANALOG ELECTRONICS (BASIC ELECTRONICS)
UNIT III ANALOG ELECTRONICS (BASIC ELECTRONICS)UNIT III ANALOG ELECTRONICS (BASIC ELECTRONICS)
UNIT III ANALOG ELECTRONICS (BASIC ELECTRONICS)
 
CCS355 Neural Network & Deep Learning UNIT III notes and Question bank .pdf
CCS355 Neural Network & Deep Learning UNIT III notes and Question bank .pdfCCS355 Neural Network & Deep Learning UNIT III notes and Question bank .pdf
CCS355 Neural Network & Deep Learning UNIT III notes and Question bank .pdf
 

rxJava 2 tips and tricks

  • 2. ABOUT ME ▸ Android developer since 2008 ▸ Lead Android Engineer @ 90Seconds.tv ▸ Kotlin and Rx addict ▸ RxKotlin contributor ▸ RxDataBindings author
  • 3. Event Iterable Observable retrieve data T next() onNext(T) discover error throw Exception onError(Exception) complete !hasNext() onCompleted() transform fromIterbale() toList() SHORTEST RX INTRO EVER
  • 4. 1.0 VS 2.0 ▸ Mar 2018 - end of rxJava1 support ▸ Better performance ▸ Lower memory consumption ▸ Can’t use null ▸ Reactive-Streams based ▸ Maybe, Flowable
  • 5. OBSERVABLE AND FRIENDS ▸ Flowable: 0..N flows, supporting Reactive-Streams and backpressure ▸ Observable: 0..N flows, no backpressure ▸ Single: a flow of exactly 1 item or an error ▸ Completable: a flow without items but only a completion or error signal ▸ Maybe: a flow with no items, exactly one item or an error
  • 6. HOW TO DEAL WITH NULLS ▸ Null object pattern ▸ Optional ▸ State wrappers
  • 7. NULL OBJECT PATTERN interface Animal { void makeSound(); } class Dog implements Animal { public void makeSound() { System.out.println("woof!"); } } enum NullAnimal implements Animal { INSTANCE; public void makeSound() {}; } void execute() { List<Animal> animals = Arrays.asList(NullAnimal.INSTANCE, new Dog()); Observable.fromIterable(animals) .filter(animal J> animal != NullAnimal.INSTANCE) .subscribe(AnimalKLmakeSound); }
  • 8. STREAM OF OPTIONAL VALUES interface Animal { void makeSound(); } class Dog implements Animal { public void makeSound() { System.out.println("woof!"); } } public void execute() { List<Optional<Animal>> animals = Arrays.asList( Optional.<Animal>empty(), Optional.of(new Dog()) ); Observable.fromIterable(animals) .filter(OptionalKLisPresent) .map(OptionalKLget) .subscribe(AnimalKLmakeSound); }
  • 9. STREAM OF STATES class AnimalStateSuccess implements AnimalState { Animal animal; AnimalStateSuccess(Animal animal) { this.animal = animal; } @Override public boolean hasAnimal() { return true;} @Override public Animal animal() { return animal; } @Override public String errorMsg() { return null; } } enum AnimalStateError implements AnimalState { INSTANCE; @Override public boolean hasAnimal() { return false;} @Override public Animal animal() { return null; } @Override public String errorMsg() { return "We lost him"; } } public void execute() { List<AnimalState> animals = Arrays.asList( AnimalStateError.INSTANCE, new AnimalStateSuccess(new Dog()) ); Observable.fromIterable(animals) .filter(AnimalStateKLhasAnimal) .map(AnimalStateKLanimal) .subscribe(AnimalKLmakeSound); }
  • 10. TESTING IS EASY ▸ Blocking calls ▸ TestSubscriber TestObserver ▸ RxJavaPlugins ▸ await()/awaitTerminalEvent() ▸ TestScheduler
  • 11. BLOCKING CALLS Observable<Integer> observable = Observable.just(1, 2); int first = observable.blockingFirst(); assertEquals(first, 1); int last = observable.blockingLast(); assertEquals(last, 2); Iterable<Integer> integers = observable.blockingIterable(); int sum = 0; for (Integer integer : integers) sum += integer; assertEquals(sum, 3);
  • 13. TEST SUBSCRIBER Observable.timer(1, TimeUnit.SECONDS) .flatMap(ignore J> Observable.just(1, 2)) .test() .assertNoErrors() .assertComplete() .assertResult(1, 2) .assertNever(3);
  • 14. TEST SUBSCRIBER Observable.timer(1, TimeUnit.SECONDS) .flatMap(ignore J> Observable.just(1, 2)) .test() .assertNoErrors() .assertComplete() .assertResult(1, 2) .assertNever(3); FAIL
  • 15. TEST SUBSCRIBER Observable.timer(1, TimeUnit.SECONDS) .flatMap(ignore J> Observable.just(1, 2)) .test() .await() .assertNoErrors() .assertComplete() .assertResult(1, 2) .assertNever(3);
  • 16. AWAIT TERMINAL EVENT boolean hasTerminalEvent = Observable.timer(1, TimeUnit.SECONDS) .flatMap(ignore J> Observable.just(1, 2)) .test() .awaitTerminalEvent(500, TimeUnit.MILLISECONDS); assertFalse(hasTerminalEvent);
  • 17. PREVENT FROM FAILING Observable.timer(1, TimeUnit.SECONDS) .flatMap(ignore J> Observable.just(1, 2)) .test() .assertNoErrors() .assertComplete() .assertResult(1, 2) .assertNever(3);
  • 18. UNIT TEST RULE @Rule final TrampolineSchedulersRule schedulers = new TrampolineSchedulersRule(); @Test public void observerTest() throws InterruptedException { Observable.timer(1, TimeUnit.SECONDS) .flatMap(ignore J> Observable.just(1, 2)) .test() .assertNoErrors() .assertComplete() .assertResult(1, 2) .assertNever(3); }
  • 19. TEST SUBSCRIBER public class TrampolineSchedulersRule implements TestRule { @Override public Statement apply(final Statement base, Description description) { return new Statement() { @Override public void evaluate() throws Throwable { RxJavaPlugins.setIoSchedulerHandler( scheduler J> Schedulers.trampoline()); RxJavaPlugins.setComputationSchedulerHandler( scheduler J> Schedulers.trampoline()); RxJavaPlugins.setNewThreadSchedulerHandler( scheduler J> Schedulers.trampoline()); try { base.evaluate(); } finally { RxJavaPlugins.reset(); } } }; } }
  • 20. TEST SUBSCRIBER public class TrampolineSchedulersRule implements TestRule { @Override public Statement apply(final Statement base, Description description) { return new Statement() { @Override public void evaluate() throws Throwable { RxJavaPlugins.setIoSchedulerHandler( scheduler J> Schedulers.trampoline()); RxJavaPlugins.setComputationSchedulerHandler( scheduler J> Schedulers.trampoline()); RxJavaPlugins.setNewThreadSchedulerHandler( scheduler J> Schedulers.trampoline()); try { base.evaluate(); } finally { RxJavaPlugins.reset(); } } }; } }
  • 21. TIME CONTROL Observable<Integer> externalObservable = Observable.timer(10, TimeUnit.SECONDS) .flatMap(ignore J> Observable.just(1, 2)); externalObservable.test() .await() .assertNoErrors() .assertComplete() .assertResult(1, 2) .assertNever(3);
  • 22. TIME CONTROL @Test public void subscriberTest() throws InterruptedException { Observable<Integer> externalObservable = Observable.timer(10, TimeUnit.SECONDS) .flatMap(ignore J> Observable.just(1, 2)); TestObserver<Integer> testObserver = externalObservable.test(); testObserver.assertNoValues(); testObserver.await() .assertNoErrors() .assertComplete() .assertResult(1, 2) .assertNever(3); }
  • 23. TIME CONTROL @Rule public TestSchedulersRule testSchedulerRule = new TestSchedulersRule(); private TestScheduler testScheduler = testSchedulerRule.testScheduler; @Test public void subscriberTest() throws InterruptedException { Observable<Integer> externalObservable = Observable.timer(10, TimeUnit.SECONDS) .flatMap(ignore J> Observable.just(1, 2)); TestObserver<Integer> testObserver = externalObservable.test(); testObserver.assertNoValues(); testScheduler.advanceTimeBy(10, TimeUnit.SECONDS); testObserver .assertNoErrors() .assertComplete() .assertResult(1, 2) .assertNever(3); }
  • 24. TEXT public class TestSchedulersRule implements TestRule { TestScheduler testScheduler = new TestScheduler(); @Override public Statement apply(final Statement base, Description description) { return new Statement() { @Override public void evaluate() throws Throwable { RxJavaPlugins.setIoSchedulerHandler( scheduler J> testScheduler); RxJavaPlugins.setComputationSchedulerHandler( scheduler J> testScheduler); RxJavaPlugins.setNewThreadSchedulerHandler( scheduler J> testScheduler); try { base.evaluate(); } finally { RxJavaPlugins.reset(); } } }; } }