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

RxJava - Be Creative with Reactive

RxJava - Be Creative with Reactive

"RxJava - Be Creative with Reactive", talk delivered in DevFest Ahmedabad 2016
http://devfest.gdgahmedabad.com/

RxJava is very powerful tool which helps Android code to understand and maintain very easily. Using RxJava, application can be created with lesser bug. With RxJava many complex tasks can be accomplished easily and without errors. As RxJava is new for Android developer, they might have questions like Why? How? When? In this talk, audience will have very useful RxJava information with example of implementing in Application.

Chintan Rathod

November 26, 2016
Tweet

More Decks by Chintan Rathod

Other Decks in Technology

Transcript

  1. AsyncTask • Must be Explicitly started • Work can not

    be modified or composed • Data is output via side effects Love it? Hate it?
  2. Example private class SomeTask extends AsyncTask < ParamType, ProgressType, ResultType

    > { protected ResultType doInBackground(ParamType...params) { /**Some code*/ } protected void onPostExecute(ResultType result) { /**Some code*/ } } new SomeTask().execute(params);
  3. Asynchronous Ideals • Explicit Execution • Easy Thread management •

    Easily Transformation • As few side effect as possible
  4. Benefits • Easy assign code to proper thread • Clearly

    understand which thread code is running on (readability) Easy Thread Management
  5. Benefits • Async tasks are often interdependent • Direct chaining

    means less room for error Easy Transformation
  6. Benefits • Code is easy to trace, and easy to

    reason about Minimize side effects
  7. RX • is Explicitly started • makes it easy to

    determine which thread work is performed on • is incredibly easy to transform and combine • has a lesser degree of side effects than many alternatives
  8. RX = Observable + LINQ + SCHEDULERS query & combine

    asynchronous data streams with operators
  9. Observables • Streams of data • Pull based • Create,

    store, pass around • Abstract away threading, synchronization, concurrency, etc
  10. Observables 1. Give me the next piece of data! 2.

    Is there any more data left to process? 3. Did any errors happen that I should know about? What do we need?
  11. Observables 1. Give me the next piece of data! =

    onNext() 2. Is there any more data left to process? 3. Did any errors happen that I should know about? What do we need?
  12. Observables 1. Give me the next piece of data! =

    onNext() 2. Is there any more data left to process? = onComplate() 3. Did any errors happen that I should know about? What do we need?
  13. Observables 1. Give me the next piece of data! =

    onNext() 2. Is there any more data left to process? = onComplate() 3. Did any errors happen that I should know about? = onError() What do we need?
  14. Quiz Time What does this stream look like?  Hello

     GDG Ahmedabad  X s.onNext(“Hello”); s.onNext(“GDG”); s.onNext(“Ahmedabad”); s.onComplete();
  15. Observable.just(1, 2, 3, 4, 5, 6) .map(new Func1<Integer, Integer>() {

    @Override public Integer call(Integer value) { return value + 1; } }); Operators
  16. Operators Observable.just(1, 2, 3, 4, 5, 6) .filter(new Func1<Integer, Boolean>()

    { @Override public Boolean call(Integer value) { return value % 2 == 1; } });
  17. Subscribe Basics • Subscribers can take different numbers of functions

    • Best practice: Always pass onError() • Use subscribeOn() and observeOn() to assign to thread
  18. Observable<Integer> observable = Observable.just(1,2,3,4,5); Subscription subscriber = observable.subscribe(new Subscriber<Integer>() {

    @Override public void onCompleted() { } @Override public void onError(Throwable e) {} @Override public void onNext(Integer integer) { Log.d("GDG","onNext : " + integer); } });
  19. Observable.just(1,2,3,4,5,6) .subscribe(next -> { Log.d("GDG","Element : " + next); });

    //Output Element : 1 Element : 2 Element : 3… Subscribe Basics
  20. Subscribe Basics Observable.just(1,2,3,4) .map( num -> num + 2 )

    .subscribe(next -> { Log.d("GDG","Element : " + next); });
  21. subscribeOn Observable.just(1,2,3) .subscribe(next -> Log.d("Home", "Element : "+ next +

    " Thread : " + Thread.currentThread().getName())); Element : 1 on thread : main Element : 2 on thread : main Element : 3 on thread : main
  22. subscribeOn Observable.just(1,2,3) .subscribeOn(Schedulers.io()) .subscribe(next -> Log.d(“GDG", "Element : " +

    next + " on thread : " + Thread.currentThread().getName())); Element : 1 on thread : RxIoScheduler-2 Element : 2 on thread : RxIoScheduler-2 Element : 3 on thread : RxIoScheduler-2
  23. subscribeOn Observable<Integer> source = Observable.range(1,3); source.map(i -> i * 100)

    .doOnNext(i -> print("Emitting " + I + " on thread " + Thread.currentThread().getName())) .observeOn(Schedulers.computation()) .map(i -> i * 10) .subscribe(i -> print("Received " + i + " on thread " + Thread.currentThread().getName()));
  24. subscribeOn - output Emitting 100 on thread main Emitting 200

    on thread main Emitting 300 on thread main Received 1000 on thread RxComputationScheduler-3 Received 2000 on thread RxComputationScheduler-3 Received 3000 on thread RxComputationScheduler-3
  25. Technology Team Lead Chintan Rathod Syx Automations Pvt. Ltd Event

    Organizer GDG Ahmedabad Follow +chintanrathod16 @chintanrathod16 Blog www.chintanrathod.com