Asynchronous Dependency Injection

A4ccb38897d103985c2e5c5eae1b4317?s=47 Eric Leong
September 26, 2017

Asynchronous Dependency Injection

Dependency injection is a useful pattern for writing modular code, but by design it requires an object's dependencies to created first, which adversely impacts your app's startup time. One solution is to use asynchronous dependency injection to create dependencies on background threads, in parallel. There are two main strategies for accomplishing this goal: Dagger Producers and RxJava with Dagger. This talk will focus on migrating slow dependencies to Dagger Producers to improve app performance.

Code: https://github.com/ericleong/bentobox
Slides: http://ericleong.me/talks/asynchronous-dependency-injection
Video: https://www.youtube.com/watch?v=c6fj_davikM

A4ccb38897d103985c2e5c5eae1b4317?s=128

Eric Leong

September 26, 2017
Tweet

Transcript

  1. 2.
  2. 3.
  3. 4.
  4. 7.
  5. 8.
  6. 9.

    @Module public class SushiModule { @Provides Fish providesFish() { return

    new Fish(); } @Provides Rice providesRice() { return new Rice(); } @Provides Sushi providesSushi(Fish fish, Rice rice) { return new Sushi(fish, rice); } }
  7. 10.

    public class BentoBoxActivity extends Activity { @Inject Sushi sushi; @Override

    protected void onCreate(Bundle savedInstanceState) { AndroidInjection.inject(this); super.onCreate(savedInstanceState); setContentView(R.layout.activity_bento_box); sushi.swim(); } }
  8. 11.
  9. 12.
  10. 13.
  11. 15.
  12. 16.
  13. 17.
  14. 21.
  15. 22.
  16. 24.
  17. 25.

    Dagger 2 @Component(modules = { AndroidBindingModule.class, SaladModule.class }) public interface

    AppComponent { void inject(App app); } @Module public class SaladModule { @Provides Salad salad(Cucumber c, Tomato t) { return new Salad(c, t); } }
  18. 26.

    RxJava 2 @Inject Lazy<Salad> saladLazy; Observable.fromCallable( new Callable<Salad>() { public

    Salad call() throws Exception { return saladLazy.get(); } }) .subscribe(new Consumer<Salad>() { public void accept(Salad salad) throws Exception { salad.dress(); } });
  19. 27.

    RxJava 2 @Module public class SaladModule { @Singleton @Provides Observable<Salad>

    providesSaladObservable(final Lazy<Salad> saladLazy) { return Observable.fromCallable(new Callable<Salad>() { @Override public Salad call() throws Exception { return saladLazy.get(); } }); } }
  20. 30.

    Dagger 2 Producers @Module public class SaladModule { @Provides Salad

    salad(Cucumber c, Tomato t) { return new Salad(c, t); } } @ProducerModule public class SaladModule { @Produces Salad salad(Cucumber c, Tomato t) { return new Salad(c, t); } }
  21. 31.

    Dagger 2 Producers @ProductionComponent( dependencies = AppComponent.class, modules = {

    ExecutorModule.class, SaladModule.class }) public interface AppProductionComponent { ListenableFuture<Salad> getSalad(); }
  22. 32.

    ListenableFuture<> App app = (App) getApplicationContext(); AppProductionComponent productionComponent = app.getAppProductionComponent();

    ListenableFuture<Salad> saladFuture = productionComponent.getSalad(); Futures.addCallback(saladFuture, new FutureCallback<Salad>() { @Override public void onSuccess(@Nullable Salad salad) { if (salad != null) { salad.dress(); } } });
  23. 33.

    Dagger 2 Production Thread(s) @Module public class ExecutorModule { @Provides

    @Production static Executor executor() { return Executors.newCachedThreadPool(); } }
  24. 34.
  25. 35.
  26. 36.
  27. 37.
  28. 42.
  29. 43.

    Deferring Until Ready public class DeferredLogger implements Logger { final

    ListenableFuture<Logger> loggerFuture; @Override public void log(final String string) { Futures.addCallback(loggerFuture, new FutureCallback<Logger>() { @Override public void onSuccess(@Nullable Logger logger) { logger.log(string); } }, MoreExecutors.directExecutor()); } }
  30. 44.

    Deferring Until Ready @Module public class LoggerModule { @Provides Logger

    providesLogger(App app) { AppProductionComponent productionComponent; productionComponent = app.getAppProductionComponent(); return new DeferredLogger(productionComponent.getLogger()); } }
  31. 45.

    LazyListenableFuture public class LazyListenableFuture<T> implements Lazy<T> { private final ListenableFuture<T>

    listenableFuture; @Override public T get() { try { return listenableFuture.get(); } catch (InterruptedException | ExecutionException e) { throw new RuntimeException("Could not get listenable.", e); } } }
  32. 47.
  33. 48.

    Reasons not to use asynchronous injection • Mental overhead of

    asynchronous dependencies • Many libraries can’t be asynchronous • Dependency tree is likely to be deep, not wide
  34. 49.

    Example: Fresco with OkHttp 1. Fresco depends on OkHttp 2.SimpleDraweeView

    requires Fresco initialization 3. Activity layout requires SimpleDraweeView Therefore… 1. OkHttp must be initialized before Activity.setContentView() 2. Main thread is blocked by OkHttp
  35. 50.