GeezerVisonThe World Wide Web, restored to 800 pixels of glory. |
| Home|About|Gaming cabinet|Archives|How it works|Starter sites |
GeezerVison is showing frogermcs.github.io — Dependency injection with Dagger 2 - Producers – froger_mcs dev blog – Coding with love {❤️} Dependency injection with Dagger 2 - ProducersThis post is a part of series of posts showing Dependency Injection with Dagger 2 framework in Android. Today we’re going to take a look at Dagger Producers - an extension to Dagger 2 that implements asynchronous dependency injection in Java. Initialization performance issuesWe all know that Dagger 2 is very well optimized framework for Dependency Injection. But even with all those micro-optimizations, still there are possible performance issues with injected dependencies - the “heavy” 3rd parties and/or our main-thread-blocking code. Maybe it’s not so important later, when our app has created serious bunch of singletons which are now served instantly by Dagger 2. But still we need a time window when we create them - and in the most cases it’s app launch time. The problem (and hints how to debug it) was described wider in one of my previous blog posts: Dagger 2 - graph creation performance. In very short, let’s imagine this case - your app has initial screen (SplashScreen) which does all needed things immediately after app launch:
Even if our code is very well optimized still there is a chance that some of external libraries need tens or hundreds of milliseconds to initialize. Before our launch screen will be presented all requested dependencies (and their dependencies) have to be initialized and delivered. It means that launch time is a sum of initialization times of each of them. Example stack measured by AndroidDevMetrics can look like this:
User will see SplashActivity in about 600ms (+ additional system work) - sum of all initialization times. Producers - asynchronous dependency injectionDagger 2 has extension named Producers which more or less solve those problems for us. Similar to Dagger synchronous injection we have a couple annotations which are used in injection process: @ProducerModuleSame as @ProducesSimilar to @ProductionComponentLike It means that each object served by Producers documentation is pretty good so there is no sens to copy it here. Instead just take a look: Dagger 2 Producers docs. The price of producersBefore we start implementation there are a couple things worth mentioning. Producers are a bit more complicated than Dagger 2 itself. It looks that mobile apps are not their main purpose of usage and it’s important to know a couple things about them:
Here you can find a good try with indirect solution for Example appIf you still want to deal with Producers let’s update our GithubClient app to use them in injection process. Before and after implementation we’ll measure app launch time with AndroidDevMetrics and compare the results. Here is the version of GithubClient app just before update with producers. And its measured average launch time looks like this:
Our plan is to handle UserManager and its all dependencies with Producers. ConfigurationWe’ll give a try to Dagger v2.1 (but Producers are also available in current version 2.0). Let’s add new version of Dagger to our project: app/build.gradle: As you can see Producers are delivered as a new dependency, next to Dagger 2 library. Also it’s worth mentioning that Dagger v2.1 finally doesn’t require Producer ModuleNow let’s move code from GithubApiProducerModule.java Looks simillar? That’s rigth, we just updated:
Production ComponentNow let’s create Again, pretty similar to ordinary Dagger’s @Component. ProductionComponent is also build in very similar way to standard Component: Additional parameter is Getting dependeciesAs I said, currently there is no way to use What is important here, object initialization starts when you call And pretty much that’s all. Of course you should remember that userManager instance will be PerformanceAt the end let’s take a look how injection performance looks like right now:
Yeah, that’s right - average value was about 15ms in this case. It’s still less than in synchronous injection (avg. 25ms) but not so little as you probably expected. It’s because Producers are not as lightweight as Dagger itself. So now it’s your decision - if it’s worth to deal with Guava, Proguard and code complexity for those kind of optimizations. Remember, if you feel that Producers are not a best fit for your app you can always try to wrap your injections with RxJava or another async code used in your app. Thanks for reading! Source codeFull source code of described project is available on Github repository. AuthorMiroslaw Stanek If you liked this post, you can share it with your followers or follow me on Twitter!
Written on March 8, 2016
|
| You are visitor 01402860 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |