introduction to rx java for android

35
Introduction to RxJava for Android Esa Firman

Upload: esa-firman

Post on 16-Jan-2017

645 views

Category:

Software


1 download

TRANSCRIPT

Page 1: Introduction to rx java for android

Introduction to RxJava for Android

Esa Firman

Page 2: Introduction to rx java for android

What is RxJava?RxJava is a Java VM implementation of ReactiveX (Reactive Extensions): a library for composing asynchronous and event-based programs by using observable sequences

See also: 2 minutes introduction to Rxhttps://medium.com/@andrestaltz/2-minute-introduction-to-rx-24c8ca793877#.rwup8ee0s

Page 3: Introduction to rx java for android

The ProblemHow to deal with execution

context ?

Page 4: Introduction to rx java for android

ThreadsHandler handler = new Handler(Looper.getMainLooper()); new Thread(){ @Override public void run() { final String result = doHeavyTask(); handler.post(new Runnable() { // or runUIThread on Activity @Override public void run() { showResult(result); } }); } }.start();

Page 5: Introduction to rx java for android

ThreadsPros:- Simple Cons:- Hard way to deliver results in UI thread- Pyramid of Doom

Page 6: Introduction to rx java for android

AsyncTasknew AsyncTask<Void, Integer, String>(){ @Override protected String doInBackground(Void... params) { return doHeavyTask(); } @Override protected void onPostExecute(String s) { showResult(s); } }.execute();

Page 7: Introduction to rx java for android

AsyncTaskPros:

- Deal with main threadCons:

- Hard way to handling error- Not bound to activity/fragment lifecycle- Not composable - Nested AsyncTask- “Antek Async”

Page 8: Introduction to rx java for android

Why Rx?- Because multithreading is hard- Execution context- Powerful operators- Composable- No Callback Hell- Etc ...

Page 9: Introduction to rx java for android

The BasicThe basic building blocks of reactive code are Observables and Subscribers. An Observable emits items; a Subscriber consumes those items.

The smallest building block is actually an Observer, but in practice you are most often using Subscriber because that's how you hook up to Observables.

Page 10: Introduction to rx java for android

The BasicObservables often don't start emitting items until someone explicitly subscribes to them

Page 11: Introduction to rx java for android

Enough talk, let’s see some code ...

Page 12: Introduction to rx java for android

Hello Worldpublic Observable<String> getStrings() { return Observable.create(new Observable.OnSubscribe<String>() { @Override public void call(Subscriber<? super String> subscriber) { try { subscriber.onNext("Hello, World"); subscriber.onCompleted(); } catch (Exception ex) { subscriber.onError(ex); } } });}

Page 13: Introduction to rx java for android

Hello WorldgetStrings().subscribe(new Subscriber(){

@Override public void onCompleted() {

// no - op} @Overridepublic void onError(Throwable e) {

e.printStackTrace();} @Override public void onNext(String s) {

System.out.println(s);}

};

Page 14: Introduction to rx java for android

Observable<String> myObservable = Observable.just("Hello, world!");

Action1<String> onNextAction = new Action1<>() { @Override public void call(String s) { System.out.println(s); }};

myObservable.subscribe(onNextAction);// Outputs "Hello, world!"

Simpler Code

Page 15: Introduction to rx java for android

Simplest* Code - Using LambdaObservable.just("Hello, World").subscribe(System.out::println);

*AFAIK

Page 16: Introduction to rx java for android

Observable CreationObservable.create(Observable.onSubscribe) from( ) — convert an Iterable or a Future or single value into an Observablerepeat( ) — create an Observable that emits a particular item or sequence of items repeatedly timer( ) — create an Observable that emits a single item after a given delayempty( ) — create an Observable that emits nothing and then completeserror( ) — create an Observable that emits nothing and then signals an errornever( ) — create an Observable that emits nothing at all

Page 17: Introduction to rx java for android

Operators

Page 18: Introduction to rx java for android

Filtering Operators- filter( ) — filter items emitted by an Observable- takeLast( ) — only emit the last n items emitted by an Observable- takeLastBuffer( ) — emit the last n items emitted by an Observable, as a single list item- skip( ) — ignore the first n items emitted by an Observable- take( ) — emit only the first n items emitted by an Observable- first( ) — emit only the first item emitted by an Observable, or the first item that meets some condition- elementAt( ) — emit item n emitted by the source Observable- timeout( ) — emit items from a source Observable, but issue an exception if no item is emitted in a

specified timespan- distinct( ) — suppress duplicate items emitted by the source Observable

Page 19: Introduction to rx java for android
Page 20: Introduction to rx java for android

FilterObservable.just("Esa", "Ganteng", "Jelek")

.filter(s -> !s.equalsIgnoreCase("jelek"))

.buffer(2)

.subscribe(strings -> {

print(strings.get(0) + “ “ +

strings.get(1));

});

// print Esa Ganteng

Page 21: Introduction to rx java for android

Transformation Operators- map( ) — transform the items emitted by an Observable by applying a function to each of them - flatMap( ) — transform the items emitted by an Observable into Observables, then flatten this into a

single Observable - scan( ) — apply a function to each item emitted by an Observable, sequentially, and emit each

successive value - groupBy( ) and groupByUntil( ) — divide an Observable into a set of Observables that emit groups of

items from the original Observable, organized by key - buffer( ) — periodically gather items from an Observable into bundles and emit these bundles rather than

emitting the items one at a time - window( ) — periodically subdivide items from an Observable into Observable windows and emit these

windows rather than emitting the items one at a time

Page 22: Introduction to rx java for android
Page 23: Introduction to rx java for android

MapObservable.from(Arrays.asList("Esa", "Esa", "Esa")) .map(s -> s + " ganteng") .subscribe(System.out::println);

// print Esa ganteng 3 times

Page 24: Introduction to rx java for android
Page 25: Introduction to rx java for android

FlatMapObservable.just( Arrays.asList("Tiramisu", "Black Forest", "Black Mamba"))

.flatMap(Observable::from)

.map(String::length)

.subscribe(System.out::print);

// print 8,11,12

Page 26: Introduction to rx java for android

Confused?

Getting map/flatMap/compose mixed up? Think types:map : T -> RflatMap : T -> Observable<R>compose : Observable<T> -> Observable<R>#ProTips

Page 27: Introduction to rx java for android

Retrofit Integration@GET("mobile_detail_calc_halte_trans.php") Observable<List<HalteDetail>> getHalteDetails(@Query("coridor") String coridor);

mApiService.getHalteDetails(halte.getCoridor()) .observeOn(AndroidSchedulers.mainThread()) .doOnTerminate(this::resetUILoading) .subscribe(halteDetails -> { if (halteDetails != null) { setMarker(halteDetails); } });

Page 28: Introduction to rx java for android

More ExamplesmApiService.getCommentList()

.compose(bindToLifecycle())

.compose(applyApiCall())

.doOnTerminate(this::stopLoading)

.subscribe(comments -> {

if (comments != null)

mCommentAdapter.pushData(comments);

});

Page 29: Introduction to rx java for android

More SamplesObservable<Bitmap> imageObservable = Observable.create(observer -> { Bitmap bm = downloadBitmap(); return bm; }); imageObservable.subscribe(image -> loadToImageView(image));

// blocking the UI thread

imageObservable .subscribeOn(Schedulers.newThread()) .observeOn(AndroidSchedulers.mainThread()) .subscribe(image -> loadToImageView(image));

// non blocking but also execute loadToImageView on UI Thread

Page 30: Introduction to rx java for android

More Examples public Observable<List<Area>> getAreas() { Observable<List<Area>> network = mApiService.getArea() .observeOn(AndroidSchedulers.mainThread()) .doOnNext(areas -> saveAreas(areas)); Observable<List<Area>> cache = getAreasFromLocal(); return Observable.concat(cache, network).first(areas -> areas != null); }

Page 31: Introduction to rx java for android

More Examples (Again)public void onLocationChanged(Location location) {

onLocationChanged.onNext(location);

}

mGmsLocationManager.onLocationChanged

.throttleLast(10, TimeUnit.SECONDS)

.observeOn(AndroidSchedulers.mainThread())

.subscribe(location -> {

mPrensenter.updateMyLocation(location);

});

Page 32: Introduction to rx java for android

FAQ1.Should we still use Thread and AsyncTask?2.Where to start?

Page 33: Introduction to rx java for android

Third Party Implementation- Android ReactiveLocation

https://github.com/mcharmas/Android-ReactiveLocation

- RxPermissions https://github.com/tbruyelle/RxPermissions

- RxBinding https://github.com/JakeWharton/RxBinding

- SQLBritehttps://github.com/square/sqlbrite

- RxLifeCyclehttps://github.com/trello/RxLifecycle

Page 34: Introduction to rx java for android

Links- http://slides.com/yaroslavheriatovych/frponandroid - https://github.com/Netflix/RxJava/- https://github.com/orfjackal/retrolambda- https://github.com/evant/gradle-retrolambda- http://cr.openjdk.java.net/~briangoetz/lambda/lambda-translation.ht

ml- https://github.com/Netflix/RxJava/wiki

Page 35: Introduction to rx java for android

Questions?