Share
Embed
Deep dive into RXJava
This Talk presents a Fundamental of RXJava in Android Application Devlopement by Soham Pandya
Technophile Community Surat
July 01, 2018
Transcript
18 slides
-
▶ RxJava – Reactive Extensions for the JVM – a
library for composing asynchronous and event-based programs using observable sequences for the Java VM. RxJava -
▶ RX = OBSERVABLE + OBSERVER + SCHEDULERS ▶ Observable.fromArray(array)
.subscribeOn(Schedulers.io()) .observeOn(AndroidSchedulers.mainThread()) .subscribe(OBSERVER); RxAndroid Structure 1. OBSERVABLE 2. OBSERVER 3. SCHEDULER -
.subscribe(new Observer<String>() { @Override public void onSubscribe(Disposable d) { }
@Override public void onNext(String s) { } @Override public void onError(Throwable e) { } @Override public void onComplete() { } }); RxAndroid Structure -
▶ IO : This is one of the most common
types of Schedulers that are used. They are generally used for IO related stuff. Such as network requests, file system operations. IO scheduler is backed by thread-pool. observable.subscribeOn(Schedulers.io()); ▶ Computation : it is good for performing small calculations and are generally quick to perform operation ▶ observable.subscribeOn(Schedulers.computation()) Schedulers -
▶ NewThread : As the name suggests, it spawns a
new thread for each active observable. This can be used to offload time consuming operation from main thread onto other thread. observable.subscribeOn(Schedulers.newThread()); ▶ Trampoline : This scheduler runs the code on current thread. So if you have a code running on the main thread, this scheduler will add the code block on the queue of main thread. observable.subscribeOn(Schedulers.trampoline()) Schedulers -
▶ Android Scheduler : This Scheduler is provided by rxAndroid library.
This is used to bring back the execution to the main thread so that UI modification can be made. This is usually used in observeOn method. It can be used by: AndroidSchedulers.mainThread(); Schedulers -
.subscribe(new Observer<String>() { @Override public void onSubscribe(Disposable d) { disposable
= d; } }); CompositeDisposable Cdisposable = new C...D...(); Cdisposable.add(disposable); Cdisposable.dispose(); RxAndroid Structure -
▶ Observable.fromArray(array) .subscribeOn(Schedulers.io()) .observeOn(AndroidSchedulers.mainThread()) .subscribe(new Observer<String>() { @Override public void
onSubscribe(Disposable d) { disposable = d; } @Override public void onNext(String s) { Log.d("data",s); } @Override public void onError(Throwable e) { } @Override public void onComplete() { Log.d("data","finished"); } }); Example 1 – OPERATOR fromArray -
▶ Using RxAndroid instead of AsyncTask ▶ Error Handling ▶
LifeCycle Changes ▶ Crashing Rotation ▶ Composing Multiple Calls Example 2 - AsyncTask -
▶ Interval 5 Seconds ▶ Do something After 10 Seconds.
▶ Do Something 5 Times ▶ Delay Start Timer Example 3 – Timer -
▶ Smarter Auto Complete ▶ only emit an item from
an Observable if a particular timespan has passed without it emitting another item. ▶ http://reactivex.io/documentation/operators/deboun ce.html Example 4 Debounce -
▶ CombileLatest ▶ when an item is emitted by either
of two Observables, combine the latest item emitted by each Observable via a specified function and emit items based on the results of this function ▶ http://reactivex.io/documentation/operators/combin elatest.html Example 5 Form Validation -
▶ Kaushik Gopal (Learning RxJava (for Android) by example) https://www.youtube.com/watch?v=k3D0cWyNno4
▶ https://medium.com/ ▶ https://reactivex.io/ Credits