Slide 1

Slide 1 text

No content

Slide 2

Slide 2 text

RxJava for Android Soham Pandya

Slide 3

Slide 3 text

▶ RxJava – Reactive Extensions for the JVM – a library for composing asynchronous and event-based programs using observable sequences for the Java VM. RxJava

Slide 4

Slide 4 text

implementation 'io.reactivex.rxjava2:rxandroid:2.0.2' implementation 'io.reactivex.rxjava2:rxjava:2.x.x‘ implementation 'com.jakewharton.rxbinding2:rxbinding:2.1.1' RxAndroid

Slide 5

Slide 5 text

▶ RX = OBSERVABLE + OBSERVER + SCHEDULERS ▶ Observable.fromArray(array) .subscribeOn(Schedulers.io()) .observeOn(AndroidSchedulers.mainThread()) .subscribe(OBSERVER); RxAndroid Structure 1. OBSERVABLE 2. OBSERVER 3. SCHEDULER

Slide 6

Slide 6 text

.subscribe(new Observer() { @Override public void onSubscribe(Disposable d) { } @Override public void onNext(String s) { } @Override public void onError(Throwable e) { } @Override public void onComplete() { } }); RxAndroid Structure

Slide 7

Slide 7 text

▶ 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 

Slide 8

Slide 8 text

▶ 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 

Slide 9

Slide 9 text

▶ 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 

Slide 10

Slide 10 text

.subscribe(new Observer() { @Override public void onSubscribe(Disposable d) { disposable = d; } }); CompositeDisposable Cdisposable = new C...D...(); Cdisposable.add(disposable); Cdisposable.dispose(); RxAndroid Structure

Slide 11

Slide 11 text

▶ Observable.fromArray(array) .subscribeOn(Schedulers.io()) .observeOn(AndroidSchedulers.mainThread()) .subscribe(new Observer() { @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

Slide 12

Slide 12 text

▶ Using RxAndroid instead of AsyncTask ▶ Error Handling ▶ LifeCycle Changes ▶ Crashing Rotation ▶ Composing Multiple Calls Example 2 - AsyncTask

Slide 13

Slide 13 text

▶ Interval 5 Seconds ▶ Do something After 10 Seconds. ▶ Do Something 5 Times ▶ Delay Start Timer Example 3 – Timer

Slide 14

Slide 14 text

▶ 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

Slide 15

Slide 15 text

▶ 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

Slide 16

Slide 16 text

▶ https://medium.com/exploring-code/what-should-y ou-know-about-rx-operators-54716a16b310 ▶ http://reactivex.io/documentation/operators.html ▶ https://medium.com/@srinuraop/rxjava-backpressur e-3376130e76c1 More Operators

Slide 17

Slide 17 text

▶ Kaushik Gopal (Learning RxJava (for Android) by example) https://www.youtube.com/watch?v=k3D0cWyNno4 ▶ https://medium.com/ ▶ https://reactivex.io/ Credits

Slide 18

Slide 18 text

Thank You