Share
Embed
RxJava2 and Dagger2
Kaushal Dhruw
August 19, 2018
Transcript
52 slides
-
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 -
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 -
Copyright © 2016 Talentica Software (I) Pvt Ltd. All rights
reserved. MVP architectural pattern -
Copyright © 2016 Talentica Software (I) Pvt Ltd. All rights
reserved. Quiz App (walkthrough video) -
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. -
Copyright © 2016 Talentica Software (I) Pvt Ltd. All rights
reserved. Reactive programming is… Observer pattern Iterator pattern Functional support And more… -
Copyright © 2016 Talentica Software (I) Pvt Ltd. All rights
reserved. Reactive programming is… Observer pattern Iterator pattern Functional support Concurrency Resilience And more… -
Copyright © 2016 Talentica Software (I) Pvt Ltd. All rights
reserved. Reactive programming Observer pattern Iterator pattern Functional support -
Copyright © 2016 Talentica Software (I) Pvt Ltd. All rights
reserved. Reactive programming Observer pattern Functional support Iterator pattern -
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 -
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 -
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. -
Copyright © 2016 Talentica Software (I) Pvt Ltd. All rights
reserved. Reactive Extensions (Rx) -
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 -
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 -
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) -
Copyright © 2016 Talentica Software (I) Pvt Ltd. All rights
reserved. Source Source (observable) Time onNext(1) onNext(2) Error / complete onNext(3) -
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 -
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() -
Copyright © 2016 Talentica Software (I) Pvt Ltd. All rights
reserved. Schedulers Source (observable) Time Subscriber (observer) Scheduler (concurrency) -
Copyright © 2016 Talentica Software (I) Pvt Ltd. All rights
reserved. Schedulers Source (observable) Time Subscriber (observer) Scheduler (concurrency) • io() • computation() • newThread() • from(Executer) -
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) -
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) -
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> -
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 -
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 -
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 -
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. -
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. -
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 -
Copyright © 2016 Talentica Software (I) Pvt Ltd. All rights
reserved. Rx Marbles Interactive diagram of Rx Observables -
Copyright © 2016 Talentica Software (I) Pvt Ltd. All rights
reserved. Operator chaining Map Filter Merge -
Copyright © 2016 Talentica Software (I) Pvt Ltd. All rights
reserved. Operator chaining Filter Reduce -
Copyright © 2016 Talentica Software (I) Pvt Ltd. All rights
reserved. Retrofit with RxJava2 Add retrofit RxJava2 adapter dependency And of course RxAndroid dependency -
Copyright © 2016 Talentica Software (I) Pvt Ltd. All rights
reserved. Retrofit with RxJava Use RxJava2 with retrofit -
Copyright © 2016 Talentica Software (I) Pvt Ltd. All rights
reserved. Room & RxJava Add Room and RxJava support dependency Supports Flowable, Single and Maybe -
Copyright © 2016 Talentica Software (I) Pvt Ltd. All rights
reserved. Room & RxJava Supports Flowable, Single and Maybe Regular RxJava2 RxJava2 ????? ????? -
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 -
Copyright © 2016 Talentica Software (I) Pvt Ltd. All rights
reserved. Dependency injection Think in terms of objects Avoid this -
Copyright © 2016 Talentica Software (I) Pvt Ltd. All rights
reserved. Dependency injection Get dependencies from outside Injecting the dependency -
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 -
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 -
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 -
Copyright © 2016 Talentica Software (I) Pvt Ltd. All rights
reserved. Types of injection • Field injection • Constructor injection • Method injection -
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 -
Copyright © 2016 Talentica Software (I) Pvt Ltd. All rights
reserved. Thank you Kaushal Dhruw @drulabs (twitter, medium, github) Q & A