reactive android: rxjava and beyond - fabio tiriticco - codemotion amsterdam 2016
TRANSCRIPT
![Page 1: Reactive Android: RxJava and beyond - Fabio Tiriticco - Codemotion Amsterdam 2016](https://reader034.vdocuments.us/reader034/viewer/2022042707/58f25cab1a28ab77588b45b9/html5/thumbnails/1.jpg)
Reactive Android: RxJava & beyond
Fabio Tiriticco / @ticofab
AMSTERDAM 11-12 MAY 2016
![Page 2: Reactive Android: RxJava and beyond - Fabio Tiriticco - Codemotion Amsterdam 2016](https://reader034.vdocuments.us/reader034/viewer/2022042707/58f25cab1a28ab77588b45b9/html5/thumbnails/2.jpg)
A little info about myself
AndroidEngineerScalaEngineer
![Page 3: Reactive Android: RxJava and beyond - Fabio Tiriticco - Codemotion Amsterdam 2016](https://reader034.vdocuments.us/reader034/viewer/2022042707/58f25cab1a28ab77588b45b9/html5/thumbnails/3.jpg)
The Reactive Amsterdam meetup
Reactive Amsterdam
Because nobody knows what Reactive means
To explore the concept in all of its forms.
![Page 4: Reactive Android: RxJava and beyond - Fabio Tiriticco - Codemotion Amsterdam 2016](https://reader034.vdocuments.us/reader034/viewer/2022042707/58f25cab1a28ab77588b45b9/html5/thumbnails/4.jpg)
“Reactive” in the vocabulary
Reactive (adjective):
Tending to act in response to an agent or influence
![Page 5: Reactive Android: RxJava and beyond - Fabio Tiriticco - Codemotion Amsterdam 2016](https://reader034.vdocuments.us/reader034/viewer/2022042707/58f25cab1a28ab77588b45b9/html5/thumbnails/5.jpg)
Reactive Confusion
“IwasthinkingofusingNode.js,butmaybeIcanbuilditwithReactive.”
“ThanksforrunningameetupaboutReact.js”
![Page 6: Reactive Android: RxJava and beyond - Fabio Tiriticco - Codemotion Amsterdam 2016](https://reader034.vdocuments.us/reader034/viewer/2022042707/58f25cab1a28ab77588b45b9/html5/thumbnails/6.jpg)
Reactive Confusion
• Theconceptof“Reactive”hascommonbasisbutslightlydifferentmeaningsineachdomain
• MostofusthinkthatReactive==React.js
Twomainobservations:
![Page 7: Reactive Android: RxJava and beyond - Fabio Tiriticco - Codemotion Amsterdam 2016](https://reader034.vdocuments.us/reader034/viewer/2022042707/58f25cab1a28ab77588b45b9/html5/thumbnails/7.jpg)
Reactive in (Android) frontend VS backend
ANDROID
UsingRxJava
WHATDOPEOPLETHINKTHAT“BEINGREACTIVE”MEANS?
BACKEND
?
![Page 8: Reactive Android: RxJava and beyond - Fabio Tiriticco - Codemotion Amsterdam 2016](https://reader034.vdocuments.us/reader034/viewer/2022042707/58f25cab1a28ab77588b45b9/html5/thumbnails/8.jpg)
Hello, RxJava!
• Asynchronousprogramming• Observablestreams
Opensourcelibrariesfor
![Page 9: Reactive Android: RxJava and beyond - Fabio Tiriticco - Codemotion Amsterdam 2016](https://reader034.vdocuments.us/reader034/viewer/2022042707/58f25cab1a28ab77588b45b9/html5/thumbnails/9.jpg)
Hello, RxJava!
Mobileengineeringishardandtherearehighexpectations.
RxJavaiscoolbecause
• itmakesitsupereasytoswitchbetweenthreads• letsusdealwithdataasastream• bringsussomedegreeoffunctionalprogramming.
![Page 10: Reactive Android: RxJava and beyond - Fabio Tiriticco - Codemotion Amsterdam 2016](https://reader034.vdocuments.us/reader034/viewer/2022042707/58f25cab1a28ab77588b45b9/html5/thumbnails/10.jpg)
RxJava goes hand in hand with Java8’s Lambdas
new Func1<String, Integer>() { @Override public Integer call(String s) { return s.length(); }}
(String s) -> { return s.length();}
s -> s.length();
RetrolambapluginforAndroid<N
![Page 11: Reactive Android: RxJava and beyond - Fabio Tiriticco - Codemotion Amsterdam 2016](https://reader034.vdocuments.us/reader034/viewer/2022042707/58f25cab1a28ab77588b45b9/html5/thumbnails/11.jpg)
RxJava: dealing with a stream of items
class Cat {...public Collar getCollar() {…}public Picture fetchPicture() {…}...
}
![Page 12: Reactive Android: RxJava and beyond - Fabio Tiriticco - Codemotion Amsterdam 2016](https://reader034.vdocuments.us/reader034/viewer/2022042707/58f25cab1a28ab77588b45b9/html5/thumbnails/12.jpg)
RxJava: dealing with a stream of items
Observable.from(myCats);(cat -> Log.d(TAG, “got cat”));
List<Cat> myCats;
Observable obs = obs.subscribe
![Page 13: Reactive Android: RxJava and beyond - Fabio Tiriticco - Codemotion Amsterdam 2016](https://reader034.vdocuments.us/reader034/viewer/2022042707/58f25cab1a28ab77588b45b9/html5/thumbnails/13.jpg)
RxJava: dealing with a stream of items
Observable.from(myCats).subscribe(cat -> Log.d(TAG, “got cat”));
List<Cat> myCats;
![Page 14: Reactive Android: RxJava and beyond - Fabio Tiriticco - Codemotion Amsterdam 2016](https://reader034.vdocuments.us/reader034/viewer/2022042707/58f25cab1a28ab77588b45b9/html5/thumbnails/14.jpg)
RxJava: work on the stream
Observable.from(myCats).map(cat -> cat.getCollar()).subscribe(collar -> Log.d(TAG, “got collar”));
map: T -> R(this map: Cat -> Collar)
![Page 15: Reactive Android: RxJava and beyond - Fabio Tiriticco - Codemotion Amsterdam 2016](https://reader034.vdocuments.us/reader034/viewer/2022042707/58f25cab1a28ab77588b45b9/html5/thumbnails/15.jpg)
RxJava: operators to manipulate the stream
Observable.from(myCats).distinct().delay(2, TimeUnit.SECONDS).filter(cat -> cat.isWhite()).subscribe(cat -> Log.d(TAG, “got white cat”));
![Page 16: Reactive Android: RxJava and beyond - Fabio Tiriticco - Codemotion Amsterdam 2016](https://reader034.vdocuments.us/reader034/viewer/2022042707/58f25cab1a28ab77588b45b9/html5/thumbnails/16.jpg)
Observable.from(myCats).subscribe(
cat -> Log.d(TAG, “cat”),error -> error.printStackTrace(),() -> Log.d(TAG, “done”)
);
RxJava: subscriber interface
Observable<T>.from(myCats).subscribe(
onNext<T>, // next item TonError, // throwableonComplete // void
);
![Page 17: Reactive Android: RxJava and beyond - Fabio Tiriticco - Codemotion Amsterdam 2016](https://reader034.vdocuments.us/reader034/viewer/2022042707/58f25cab1a28ab77588b45b9/html5/thumbnails/17.jpg)
Observable.from(myCats) .subscribe(cat -> Log.d(TAG, “cat”));
RxJava: unsubscribe
Subscription subs =
subs.unsubscribe();
![Page 18: Reactive Android: RxJava and beyond - Fabio Tiriticco - Codemotion Amsterdam 2016](https://reader034.vdocuments.us/reader034/viewer/2022042707/58f25cab1a28ab77588b45b9/html5/thumbnails/18.jpg)
RxJava: threading
Observable.from(myCats)
.map(cat -> cat.fetchPicture())
.map(picture -> Filter.applyFilter(picture))
.subscribe(filteredPicture -> display(filteredPicture)
);
.subscribeOn(Schedulers.newThread())
.observeOn(AndroidSchedulers.mainThread())
![Page 19: Reactive Android: RxJava and beyond - Fabio Tiriticco - Codemotion Amsterdam 2016](https://reader034.vdocuments.us/reader034/viewer/2022042707/58f25cab1a28ab77588b45b9/html5/thumbnails/19.jpg)
RxJava: other ways of creating Observables
// emits on single item and completesObservable.just
// emits one item after a certain delayObservable.timer
.. plus many others, and you can create your own!
![Page 20: Reactive Android: RxJava and beyond - Fabio Tiriticco - Codemotion Amsterdam 2016](https://reader034.vdocuments.us/reader034/viewer/2022042707/58f25cab1a28ab77588b45b9/html5/thumbnails/20.jpg)
RxJava: demo with Retrofit & Meetup Streams
http://stream.meetup.com/2/rsvp
Meetup Streaming API
![Page 21: Reactive Android: RxJava and beyond - Fabio Tiriticco - Codemotion Amsterdam 2016](https://reader034.vdocuments.us/reader034/viewer/2022042707/58f25cab1a28ab77588b45b9/html5/thumbnails/21.jpg)
RxJava: demo with Meetup Streams
![Page 22: Reactive Android: RxJava and beyond - Fabio Tiriticco - Codemotion Amsterdam 2016](https://reader034.vdocuments.us/reader034/viewer/2022042707/58f25cab1a28ab77588b45b9/html5/thumbnails/22.jpg)
RxJava: demo with Retrofit & Meetup Streams
interface MeetupAPI { @GET("http://stream.meetup.com/2/rsvp") @Streaming Observable<ResponseBody> meetupStream();}
![Page 23: Reactive Android: RxJava and beyond - Fabio Tiriticco - Codemotion Amsterdam 2016](https://reader034.vdocuments.us/reader034/viewer/2022042707/58f25cab1a28ab77588b45b9/html5/thumbnails/23.jpg)
RxJava: demo with RSVP Meetup Streams
meetupAPI.meetupStream()....flatMap(responseBody -> events(responseBody.source())).map(string -> gson.fromJson(string, RSVP.class))... .subscribe(rsvp -> ...);
map: T -> RflatMap: T -> Observable<R>
![Page 24: Reactive Android: RxJava and beyond - Fabio Tiriticco - Codemotion Amsterdam 2016](https://reader034.vdocuments.us/reader034/viewer/2022042707/58f25cab1a28ab77588b45b9/html5/thumbnails/24.jpg)
RxJava: demo with Meetup Streams
![Page 25: Reactive Android: RxJava and beyond - Fabio Tiriticco - Codemotion Amsterdam 2016](https://reader034.vdocuments.us/reader034/viewer/2022042707/58f25cab1a28ab77588b45b9/html5/thumbnails/25.jpg)
RxJava: demo with Meetup Streams
https://github.com/ticofab/android-meetup-streams
![Page 26: Reactive Android: RxJava and beyond - Fabio Tiriticco - Codemotion Amsterdam 2016](https://reader034.vdocuments.us/reader034/viewer/2022042707/58f25cab1a28ab77588b45b9/html5/thumbnails/26.jpg)
Reactive in (Android) frontend VS backend
ANDROID
UsingRxJava
WHATDOPEOPLETHINKTHAT“BEINGREACTIVE”MEANS?
BACKEND
?
![Page 27: Reactive Android: RxJava and beyond - Fabio Tiriticco - Codemotion Amsterdam 2016](https://reader034.vdocuments.us/reader034/viewer/2022042707/58f25cab1a28ab77588b45b9/html5/thumbnails/27.jpg)
Evolution of server applications & user expectations
2006 2016
Servers ~10 Theskyisthelimit.
Responsetime seconds milliseconds
Offlinemaintenance hours what?
Dataamount Gigabytes Petabytes
Machines Singlecore,littledistribution Mustworkacrossasyncboundaries(location,threads)
Kindofdata Request-response Streams(endless)
![Page 28: Reactive Android: RxJava and beyond - Fabio Tiriticco - Codemotion Amsterdam 2016](https://reader034.vdocuments.us/reader034/viewer/2022042707/58f25cab1a28ab77588b45b9/html5/thumbnails/28.jpg)
Evolution of internet usage
![Page 29: Reactive Android: RxJava and beyond - Fabio Tiriticco - Codemotion Amsterdam 2016](https://reader034.vdocuments.us/reader034/viewer/2022042707/58f25cab1a28ab77588b45b9/html5/thumbnails/29.jpg)
The Reactive traits
Areactivecomputersystemmust Trait
Reacttoitsusers Responsive
Reacttofailureandstayavailable Resilient
Reacttovaryingloadconditions Elastic
Itscomponentsmustreacttoinputs Message-driven
![Page 30: Reactive Android: RxJava and beyond - Fabio Tiriticco - Codemotion Amsterdam 2016](https://reader034.vdocuments.us/reader034/viewer/2022042707/58f25cab1a28ab77588b45b9/html5/thumbnails/30.jpg)
The Reactive Manifesto
Responsive
ResilientElastic
MessageDriven
![Page 31: Reactive Android: RxJava and beyond - Fabio Tiriticco - Codemotion Amsterdam 2016](https://reader034.vdocuments.us/reader034/viewer/2022042707/58f25cab1a28ab77588b45b9/html5/thumbnails/31.jpg)
Reactive traits: Responsive
• Ahumanwhovisitsawebsite• Aclientwhichmakesarequesttoaserver• Aconsumerwhichcontactsaprovider• ...
AReactivesystemrespondstoinputsandusagefromitsuser.
![Page 32: Reactive Android: RxJava and beyond - Fabio Tiriticco - Codemotion Amsterdam 2016](https://reader034.vdocuments.us/reader034/viewer/2022042707/58f25cab1a28ab77588b45b9/html5/thumbnails/32.jpg)
Reactive traits: Elastic
• ScaleOUTandIN:usejusttherightamount• Elasticityreliesondistribution• Ensurereplicationincaseoffailure
Scaleondemandtoreacttovaryingload
![Page 33: Reactive Android: RxJava and beyond - Fabio Tiriticco - Codemotion Amsterdam 2016](https://reader034.vdocuments.us/reader034/viewer/2022042707/58f25cab1a28ab77588b45b9/html5/thumbnails/33.jpg)
Reactive traits: Resilient
Itdoesn'tmatterhowgreatyourapplicationisifitdoesn'twork.
FAULTTOLERANCE RESILIENCEVS
![Page 34: Reactive Android: RxJava and beyond - Fabio Tiriticco - Codemotion Amsterdam 2016](https://reader034.vdocuments.us/reader034/viewer/2022042707/58f25cab1a28ab77588b45b9/html5/thumbnails/34.jpg)
Reactive traits: Message Driven
ReactivesystemsdesignconcentratesonMessages.
Asynchronousmessagingenables:
• Separationbetweencomponents• Errorcontainment-avoidchainfailures• Domainmappingclosertoreality
![Page 35: Reactive Android: RxJava and beyond - Fabio Tiriticco - Codemotion Amsterdam 2016](https://reader034.vdocuments.us/reader034/viewer/2022042707/58f25cab1a28ab77588b45b9/html5/thumbnails/35.jpg)
The Reactive Manifesto, take 2
Responsive
ResilientElastic
MessageDriven
![Page 36: Reactive Android: RxJava and beyond - Fabio Tiriticco - Codemotion Amsterdam 2016](https://reader034.vdocuments.us/reader034/viewer/2022042707/58f25cab1a28ab77588b45b9/html5/thumbnails/36.jpg)
Why Functional Programming?
Moresupportiveofreasoningaboutproblemsinconcurrentandparallelisedapplications.
• encouragesuseofpurefunctions-minimisesideeffects• encouragesimmutability-statedoesn’tgetpassedaround• higher-orderfunctions-reusabilityandcomposability
![Page 37: Reactive Android: RxJava and beyond - Fabio Tiriticco - Codemotion Amsterdam 2016](https://reader034.vdocuments.us/reader034/viewer/2022042707/58f25cab1a28ab77588b45b9/html5/thumbnails/37.jpg)
Reactive Patterns
DesignpatternstoachieveReactiveprinciples.
Toolkitsexisttoimplementthesepatterns.
![Page 38: Reactive Android: RxJava and beyond - Fabio Tiriticco - Codemotion Amsterdam 2016](https://reader034.vdocuments.us/reader034/viewer/2022042707/58f25cab1a28ab77588b45b9/html5/thumbnails/38.jpg)
Reactive Pattern: Simple Component Pattern
“Onecomponentshoulddoonlyonethingbutdoitinfull.Theaimistomaximisecohesionandminimisecouplingbetweencomponents.”
![Page 39: Reactive Android: RxJava and beyond - Fabio Tiriticco - Codemotion Amsterdam 2016](https://reader034.vdocuments.us/reader034/viewer/2022042707/58f25cab1a28ab77588b45b9/html5/thumbnails/39.jpg)
Reactive Pattern: Simple Component Pattern
Actor1
Actor3
Actor2
• containsstate• hasamailboxtoreceiveandsendmessages
• containsbehaviourlogic• hasasupervisor
ActormodelSupervisor
![Page 40: Reactive Android: RxJava and beyond - Fabio Tiriticco - Codemotion Amsterdam 2016](https://reader034.vdocuments.us/reader034/viewer/2022042707/58f25cab1a28ab77588b45b9/html5/thumbnails/40.jpg)
Reactive Patterns: Let it crash!
"Preferafullcomponentrestarttocomplexinternalfailurehandling".
• failureconditionsWILLoccur• theymightberareandhardtoreproduce• itismuchbettertostartcleanthantotrytorecover• …whichmightbeexpensiveanddifficult!
![Page 41: Reactive Android: RxJava and beyond - Fabio Tiriticco - Codemotion Amsterdam 2016](https://reader034.vdocuments.us/reader034/viewer/2022042707/58f25cab1a28ab77588b45b9/html5/thumbnails/41.jpg)
![Page 42: Reactive Android: RxJava and beyond - Fabio Tiriticco - Codemotion Amsterdam 2016](https://reader034.vdocuments.us/reader034/viewer/2022042707/58f25cab1a28ab77588b45b9/html5/thumbnails/42.jpg)
Reactive Patterns: Let it crash!
• Componentsshouldbeisolated-stateisnotshared• Componentsshouldhaveasupervisoranddelegatetoitsomeorallerrorhandling
• Thesupervisorcantransparentlyrestartthecomponent• Message-passingarchitecturesenforcestateconfinement,errorcontainmentandtransparency
Inpractice…
![Page 43: Reactive Android: RxJava and beyond - Fabio Tiriticco - Codemotion Amsterdam 2016](https://reader034.vdocuments.us/reader034/viewer/2022042707/58f25cab1a28ab77588b45b9/html5/thumbnails/43.jpg)
Reactive Patterns: Let it crash!
Actorsupervisionexample
Actor1
Actor2
Supervisor
WhateverException!
X
FixorRestart
![Page 44: Reactive Android: RxJava and beyond - Fabio Tiriticco - Codemotion Amsterdam 2016](https://reader034.vdocuments.us/reader034/viewer/2022042707/58f25cab1a28ab77588b45b9/html5/thumbnails/44.jpg)
Reactive Patterns: Let it crash!
• themachineonlyacceptsexactchange
![Page 45: Reactive Android: RxJava and beyond - Fabio Tiriticco - Codemotion Amsterdam 2016](https://reader034.vdocuments.us/reader034/viewer/2022042707/58f25cab1a28ab77588b45b9/html5/thumbnails/45.jpg)
Reactive Patterns: Let it crash!
Scenario1:Theuserinsertswrongamountofcoins
X
Error!
![Page 46: Reactive Android: RxJava and beyond - Fabio Tiriticco - Codemotion Amsterdam 2016](https://reader034.vdocuments.us/reader034/viewer/2022042707/58f25cab1a28ab77588b45b9/html5/thumbnails/46.jpg)
Reactive Patterns: Let it crash!
Scenario2:Themachineisoutofcoffeebeans
Failure!(!=error)
![Page 47: Reactive Android: RxJava and beyond - Fabio Tiriticco - Codemotion Amsterdam 2016](https://reader034.vdocuments.us/reader034/viewer/2022042707/58f25cab1a28ab77588b45b9/html5/thumbnails/47.jpg)
Reactive Patterns: Let it crash!
![Page 48: Reactive Android: RxJava and beyond - Fabio Tiriticco - Codemotion Amsterdam 2016](https://reader034.vdocuments.us/reader034/viewer/2022042707/58f25cab1a28ab77588b45b9/html5/thumbnails/48.jpg)
Reactive Patterns: Let it crash!
RandomlykillsinstancesoftheirAWSsystemtoensurethatnofailureispropagated.
![Page 49: Reactive Android: RxJava and beyond - Fabio Tiriticco - Codemotion Amsterdam 2016](https://reader034.vdocuments.us/reader034/viewer/2022042707/58f25cab1a28ab77588b45b9/html5/thumbnails/49.jpg)
BACKEND
?
BACKEND
Elasticity
Asyncrhonicity
Resilience
Supervision
Messagepassing
Stateencapsulation
Streams
Backpressure
…
Reactive in (Android) frontend VS backend
ANDROID
UsingRxJava
WHATDOPEOPLETHINKTHAT“BEINGREACTIVE”MEANS?
![Page 50: Reactive Android: RxJava and beyond - Fabio Tiriticco - Codemotion Amsterdam 2016](https://reader034.vdocuments.us/reader034/viewer/2022042707/58f25cab1a28ab77588b45b9/html5/thumbnails/50.jpg)
Reactive traits in Android?
CanweapplysomeoftheReactiveManifestoprinciplestoourAndroiddevelopment?
![Page 51: Reactive Android: RxJava and beyond - Fabio Tiriticco - Codemotion Amsterdam 2016](https://reader034.vdocuments.us/reader034/viewer/2022042707/58f25cab1a28ab77588b45b9/html5/thumbnails/51.jpg)
Reactive traits in Android?
Reactivetrait InAndroid?
Responsive Executeasmuchaspossibleasynchronously
Elastic —
Resilient Delegateriskystuffto(Intent)Servicesorisolatedcomponents
MessagepassingCommunicationviaResultReceiver
UsesomeeventBus
![Page 52: Reactive Android: RxJava and beyond - Fabio Tiriticco - Codemotion Amsterdam 2016](https://reader034.vdocuments.us/reader034/viewer/2022042707/58f25cab1a28ab77588b45b9/html5/thumbnails/52.jpg)
Reactive traits in Android?
…butIamsurethatwecantakethisfurther.
![Page 53: Reactive Android: RxJava and beyond - Fabio Tiriticco - Codemotion Amsterdam 2016](https://reader034.vdocuments.us/reader034/viewer/2022042707/58f25cab1a28ab77588b45b9/html5/thumbnails/53.jpg)
Resources
https://github.com/ReactiveX/RxJava/wiki RxJavadocumentation&wiki
http://rxmarbles.com RxJavaoperatorsexplainedvisually
http://www.reactivemanifesto.org Thereactivemanifesto
https://www.youtube.com/watch?v=fNEZtx1VVAkhttps://www.youtube.com/watch?v=ryIAibBibQIhttps://www.youtube.com/watch?v=JvbUF33sKf8
TheReactiveRevealedseries:awesomewebinarsbythecreatorsoftheReactiveManifesto.
https://www.manning.com/books/reactive-design-patternshttps://www.youtube.com/watch?v=nSfXcSWq0ug Reactivedesignpatterns,bookandwebinar.
![Page 54: Reactive Android: RxJava and beyond - Fabio Tiriticco - Codemotion Amsterdam 2016](https://reader034.vdocuments.us/reader034/viewer/2022042707/58f25cab1a28ab77588b45b9/html5/thumbnails/54.jpg)
Thanks!
@ticofab
All pictures belong to their respective authors
AMSTERDAM 11-12 MAY 2016