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

RxJava2 and Dagger2

RxJava2 and Dagger2

Kaushal Dhruw

August 19, 2018
Tweet

More Decks by Kaushal Dhruw

Other Decks in Programming

Transcript

  1. Copyright © 2016 Talentica Software (I) Pvt Ltd. All rights

    reserved. Contents • MVP brief intro • Reactive programming • Retrofit with RxJava2 • Room with RxJava2 • Dependency injection & Dagger • Putting it all together 2
  2. Copyright © 2016 Talentica Software (I) Pvt Ltd. All rights

    reserved. What’s NOT covered x Room x Retrofit x Advanced concepts x Testing x MVP, MVVM from scratch x Step by step walk through x In-depth knowledge of RxJava2 and Dagger2 3
  3. Copyright © 2016 Talentica Software (I) Pvt Ltd. All rights

    reserved. Quiz App (walkthrough video)
  4. Copyright © 2016 Talentica Software (I) Pvt Ltd. All rights

    reserved. Reactive programming • Wikipedia – It is an asynchronous programming paradigm concerned with data flows and propagation of change. • In other words: – Reactive programming is programming with asynchronous data streams.
  5. Copyright © 2016 Talentica Software (I) Pvt Ltd. All rights

    reserved. Reactive programming is… Observer pattern Iterator pattern Functional support And more…
  6. Copyright © 2016 Talentica Software (I) Pvt Ltd. All rights

    reserved. Reactive programming is… Observer pattern Iterator pattern Functional support Concurrency Resilience And more…
  7. Copyright © 2016 Talentica Software (I) Pvt Ltd. All rights

    reserved. Reactive programming Observer pattern Iterator pattern Functional support
  8. Copyright © 2016 Talentica Software (I) Pvt Ltd. All rights

    reserved. Reactive programming Observer pattern Functional support Iterator pattern
  9. Copyright © 2016 Talentica Software (I) Pvt Ltd. All rights

    reserved. Reactive programming Observer pattern Iterator pattern Functional support Create Combine Transform Chain Listen Filter Reduce Map
  10. Copyright © 2016 Talentica Software (I) Pvt Ltd. All rights

    reserved. Reactive Extensions (Rx) • Reactive extensions (or Rx) is a library for composing asynchronous and event-based programs by using observable sequences
  11. Copyright © 2016 Talentica Software (I) Pvt Ltd. All rights

    reserved. Reactive Extensions (Rx) • Reactive extensions (or Rx) is a library for composing and programs by using sequences simultaneous operations. perform some operation(s) when event is triggered or received. think of it as asynchronous immutable array.
  12. Copyright © 2016 Talentica Software (I) Pvt Ltd. All rights

    reserved. RxJava & RxAndroid • RxJava is a Java VM implementation of ReactiveX. • RxAndroid provides android specific bindings and extensions. So RxJava can be used in android applications
  13. Copyright © 2016 Talentica Software (I) Pvt Ltd. All rights

    reserved. Components • Source (emits data) – Emits data streams • Subscriber (the observer or consumer) – Consumes data emitted from source • Scheduler (threads and concurrency) – Where to process and consume data
  14. Copyright © 2016 Talentica Software (I) Pvt Ltd. All rights

    reserved. Components Source (observable) Subscriber (observer) Scheduler (concurrency) subscribe dispose Time onNext(1) onNext(2) Error / complete onNext(3)
  15. Copyright © 2016 Talentica Software (I) Pvt Ltd. All rights

    reserved. Source Source (observable) Time onNext(1) onNext(2) Error / complete onNext(3)
  16. Copyright © 2016 Talentica Software (I) Pvt Ltd. All rights

    reserved. Source & Subscriber Source (observable) Time onNext(1) onNext(2) Error / complete onNext(3) Subscriber (observer) subscribe dispose
  17. Copyright © 2016 Talentica Software (I) Pvt Ltd. All rights

    reserved. Schedulers Source (observable) Time Subscriber (observer) Scheduler (concurrency) Thread t = new Thread(…); t.start(); Wait() Notify()
  18. Copyright © 2016 Talentica Software (I) Pvt Ltd. All rights

    reserved. Schedulers Source (observable) Time Subscriber (observer) Scheduler (concurrency)
  19. Copyright © 2016 Talentica Software (I) Pvt Ltd. All rights

    reserved. Schedulers Source (observable) Time Subscriber (observer) Scheduler (concurrency) • io() • computation() • newThread() • from(Executer)
  20. Copyright © 2016 Talentica Software (I) Pvt Ltd. All rights

    reserved. Source, Subscriber & Scheduler Source (observable) Time onNext(1) onNext(2) Error / complete onNext(3) Subscriber (observer) subscribe dispose Scheduler (concurrency)
  21. Copyright © 2016 Talentica Software (I) Pvt Ltd. All rights

    reserved. Components Source (observable) Subscriber (observer) Scheduler (concurrency) subscribe dispose Time onNext(1) onNext(2) Error / complete onNext(3)
  22. Copyright © 2016 Talentica Software (I) Pvt Ltd. All rights

    reserved. Source • Observable<T> • Flowable<T> • Single<T> • Maybe<T> • Completable<T> • ObservableSource<T> • Publisher<T> • SingleSource<T> • MaybeSource<T> • CompletableSource<T>
  23. Copyright © 2016 Talentica Software (I) Pvt Ltd. All rights

    reserved. Observable • Observable<T> • Flowable<T> • Single<T> • Maybe<T> • Completable<T> Emits 0 to N items terminates with success or exception
  24. Copyright © 2016 Talentica Software (I) Pvt Ltd. All rights

    reserved. Flowable • Observable<T> • Flowable<T> • Single<T> • Maybe<T> • Completable<T> Emits 0 to N items terminates with success or exception. Supports backpressure
  25. Copyright © 2016 Talentica Software (I) Pvt Ltd. All rights

    reserved. Single • Observable<T> • Flowable<T> • Single<T> • Maybe<T> • Completable<T> Emits single item or error. Has no completion indicator
  26. Copyright © 2016 Talentica Software (I) Pvt Ltd. All rights

    reserved. Maybe • Observable<T> • Flowable<T> • Single<T> • Maybe<T> • Completable<T> Succeeds with 0 or 1 item, or fails. Has completion indicator. Reactive optional.
  27. Copyright © 2016 Talentica Software (I) Pvt Ltd. All rights

    reserved. Completable • Observable<T> • Flowable<T> • Single<T> • Maybe<T> • Completable<T> Completes with success or fails. No emission.
  28. Copyright © 2016 Talentica Software (I) Pvt Ltd. All rights

    reserved. RxJava Operators • Creational – Create – Just – From • Transformational – Map – Flatmap – Group by • Filter – Filter – Distinct – Skip • Combine – Concat – Merge – Zip
  29. Copyright © 2016 Talentica Software (I) Pvt Ltd. All rights

    reserved. Rx Marbles Interactive diagram of Rx Observables
  30. Copyright © 2016 Talentica Software (I) Pvt Ltd. All rights

    reserved. Operator chaining Map Filter Merge
  31. Copyright © 2016 Talentica Software (I) Pvt Ltd. All rights

    reserved. Operator chaining Filter Reduce
  32. Copyright © 2016 Talentica Software (I) Pvt Ltd. All rights

    reserved. Retrofit with RxJava2 Add retrofit RxJava2 adapter dependency And of course RxAndroid dependency
  33. Copyright © 2016 Talentica Software (I) Pvt Ltd. All rights

    reserved. Retrofit with RxJava Use RxJava2 with retrofit
  34. Copyright © 2016 Talentica Software (I) Pvt Ltd. All rights

    reserved. Room & RxJava Add Room and RxJava support dependency Supports Flowable, Single and Maybe
  35. Copyright © 2016 Talentica Software (I) Pvt Ltd. All rights

    reserved. Room & RxJava Supports Flowable, Single and Maybe Regular RxJava2 RxJava2 ????? ?????
  36. Copyright © 2016 Talentica Software (I) Pvt Ltd. All rights

    reserved. Dependency injection Got all the ingredients for pizza except the base Should I create pizza base myself? You are gonna mess it up let me do it
  37. Copyright © 2016 Talentica Software (I) Pvt Ltd. All rights

    reserved. Dependency injection Think in terms of objects Avoid this
  38. Copyright © 2016 Talentica Software (I) Pvt Ltd. All rights

    reserved. Dependency injection Get dependencies from outside Injecting the dependency
  39. Copyright © 2016 Talentica Software (I) Pvt Ltd. All rights

    reserved. Dependency injection Instead of having hidden dependencies, pass the dependent objects • loose coupling • Testable code • Reusable code • Readable code • Enforces SRP
  40. Copyright © 2016 Talentica Software (I) Pvt Ltd. All rights

    reserved. Dagger2 Dagger is a compile time dependency injection framework for both Java and Android. I will inject the dependencies just tell me: • What dependencies to inject - @Component • How to create it - @Module, @Provides • Where to inject - @Inject • Which one to inject - @Qualifier, @Named
  41. Copyright © 2016 Talentica Software (I) Pvt Ltd. All rights

    reserved. Dagger dependency graph Pizza Pizza base Chicken sausage Cheese Flour Chicken Milk salt Processed chicken Toppings Tomato chilli flakes oregano
  42. Copyright © 2016 Talentica Software (I) Pvt Ltd. All rights

    reserved. Types of injection • Field injection • Constructor injection • Method injection
  43. Copyright © 2016 Talentica Software (I) Pvt Ltd. All rights

    reserved. Summary • Brief introduction of MVP • Reactive programming – What, why and how? – Various operators – With Room and Retrofit • Dagger • How these work together
  44. Copyright © 2016 Talentica Software (I) Pvt Ltd. All rights

    reserved. Thank you Kaushal Dhruw @drulabs (twitter, medium, github) Q & A