Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Deep dive into RXJava

Deep dive into RXJava

This Talk presents a Fundamental of RXJava in Android Application Devlopement by Soham Pandya

More Decks by Technophile Community Surat

Other Decks in Programming

Transcript

  1. ▶ RxJava – Reactive Extensions for the JVM – a

    library for composing asynchronous and event-based programs using observable sequences for the Java VM. RxJava
  2. ▶ RX = OBSERVABLE + OBSERVER + SCHEDULERS ▶ Observable.fromArray(array)

    .subscribeOn(Schedulers.io()) .observeOn(AndroidSchedulers.mainThread()) .subscribe(OBSERVER); RxAndroid Structure 1. OBSERVABLE 2. OBSERVER 3. SCHEDULER
  3. .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
  4. ▶ 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 
  5. ▶ 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 
  6. ▶ 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 
  7. .subscribe(new Observer<String>() { @Override public void onSubscribe(Disposable d) { disposable

    = d; } }); CompositeDisposable Cdisposable = new C...D...(); Cdisposable.add(disposable); Cdisposable.dispose(); RxAndroid Structure
  8. ▶ 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
  9. ▶ Using RxAndroid instead of AsyncTask ▶ Error Handling ▶

    LifeCycle Changes ▶ Crashing Rotation ▶ Composing Multiple Calls Example 2 - AsyncTask
  10. ▶ Interval 5 Seconds ▶ Do something After 10 Seconds.

    ▶ Do Something 5 Times ▶ Delay Start Timer Example 3 – Timer
  11. ▶ 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
  12. ▶ 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