10

This is a more theoretical question. Please let me know if I'm going in the wrong direction.

Is there any way to load some of graph dependencies asynchronously/in parallel in Dagger 2? Should it be even considered in a context of Dagger ?

My problem is mainly connected with app launch time. A lot of external dependencies like Mixpanel, Crashlytics/Fabric, Retrofit (RestAdapter) cause that the app is warming up longer than 1 second.

What helped me a lot is Lazy<> interface, but the final effect still doesn't satisfy me.

Any ideas?

Example

App has SplashActivity which depends on SplashActivityPresenter, which depends on: Mixpanel, RestAdapter and Crashlytics libraries (and a couple "smaller" objects). Each of them has .init() method which takes a lot of time (Mixpanel initialization takes about 200ms on Nexus 5, Android M. So in result it will take about 2 seconds before user sees Splash screen.

Is there any way to Construct those objects in parallel ?

7
  • Correct me if I'm wrong, but Dagger 2 relies on generated code, so it shouldn't affect startup time.
    – Egor
    Sep 23, 2015 at 11:47
  • Graph composition is done in compile time but objects creation happens in runtime. So objects initialisation (construction and optional configuration) happens after we launch the app (in a moment when we're injecting them for the first time).
    – froger_mcs
    Sep 23, 2015 at 12:11
  • Why does your SplashActivity depend on those libraries? Does it really need to? If not then you could show splash before you create those objects but it doesn't solve the problem. 2sec is long even for splash screen imho.
    – Michał Z.
    Sep 23, 2015 at 18:47
  • It's just simplified version of dependencies graph used to describe the problem. In real solution it would be: Presenter depends on AnalyticsManager, UserManager. AnalyticsManager depends on GoogleAnalytics and Mixpanel, UserManager depends on LocalStoreManager and RestAdapter. And so on...
    – froger_mcs
    Sep 23, 2015 at 18:50
  • I got it but I don't understand why do you need to have all of these in your SplashActivity. Couldn't SplashActivity be a simple Activity that has no dependencies and create all Managers after it's created?
    – Michał Z.
    Sep 23, 2015 at 19:08

2 Answers 2

9
+25

Keep the graph creation synchronous and defer object creation until subscribed to with Rx's defer. Downstream injections can be notified via a Subject, upon the object's creation.

@Module public static class AppModule{

    @Provides @Singleton
    public Observable<A> provideA(){
        return Observable.defer(
                () -> Observable.just(new A()) 
        );
    }
}

public class MainActivity extends AppCompatActivity{

    @Inject Observable<A> aObservable;
    A a;
    AppComponent appComponent;

    @Override
    protected void onCreate(Bundle savedInstanceState){
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ButterKnife.bind(this);

        if(appComponent == null){ 
            appComponent = ((App) getApplication())
                .getAppComponent(); 
        }
        appComponent.inject(this);

        aObservable.subscribeOn(Schedulers.io())
                   .observeOn(AndroidSchedulers.mainThread())
                   .subscribe(new Action1<A>(){
                       @Override public void call(final A aa){
                           a = aa;
                           Log.d("MainActivity", "a = aa");
                       }
                   });
    }
}

Caveats: must manually check for object !null, or rely on the Subject acting as an eventbus, which would pass down the boolean status of the object that downstream injected objects must wait for.

1
  • This solution is a good way for me but in this case you should care about not yet initialized objects, as you mentioned. It would work in case when you use some dependencies once, but when you should care about it everywhere (e.g. is used in some activities, presenters, services etc.) not null check should be done somewhere else. I'm just working on temporary solution for this, but probably the final final will need some annotation processing.
    – froger_mcs
    Sep 28, 2015 at 20:11
1

There's nothing special about Dagger 2's object graph creation. If you want it to be done in a background thread, just call DaggerYourComponent.create() or DaggerYourComponent.Builder.build() on a background thread (use your preferred method to do this - e.g. an AsyncTask).

If you have any @Inject constructors which assume they will be run on the UI thread, then you'll have to modify those, but otherwise you shouldn't have any issues.

4
  • 1
    Place of object construction isn't my concern. Main issue I have to face with is that graph is constructed one object after another. App launch takes 200ms of Mixpanel.init + 200ms of Crashlytics.init + 200ms of RestAdapter.init + (...) I'd like to construct those objects in one time, in parallel.
    – froger_mcs
    Sep 25, 2015 at 16:33
  • 4
    You might want to update your question then - you asked for asynchronous construction, not construction in parallel. Sep 25, 2015 at 18:21
  • By asynchronous I meant more than one thing at time, not on the other thread (because it's still synchronous in my opinion).
    – froger_mcs
    Sep 25, 2015 at 18:30
  • @froger_mcs I agree with vaughandroid, your question does suggest that you want to somehow do everything in background thread and defer app/activity/component creation. If you want to call inits in parallel, why not just put them in separate modules, create them on background scheduler and combine with RxJava to get 3 at once? Can't really provide proper answer now, but I believe if you edited your question it would be clearer what you want to achieve
    – wasyl
    Sep 28, 2015 at 21:37

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.