sharonooo
sharonooo

Reputation: 694

Provide singleton dependency vs object initialization

I have two questions about Dagger2 and it's Singleton scope,

I used Retrofit and GSON but it's just for the example, I want to make sure I understand Dagger2 correctly.

I have NetowrkModule like:

@Module
public class NetworkModule {

    @Provides
    @Singleton
    Retrofit provideRetrofit(OkHttpClient okHttpClient, GsonConverterFactory gsonConverterFactory) {
        return new Retrofit.Builder()
                .addConverterFactory(gsonConverterFactory)
                .baseUrl("https://example.com")
                .client(okHttpClient)
                .addCallAdapterFactory(RxJava2CallAdapterFactory.create())
                .build();
    }

    @Singleton
    @Provides
    OkHttpClient provideOkHttpClient() {
        return new OkHttpClient.Builder().build();
    }

    @Singleton
    @Provides
    GsonConverterFactory provideGson() {
        return GsonConverterFactory.create();
    }
}

and ApplicationComponent:

@Singleton
@Component(modules = {
        AndroidSupportInjectionModule.class,
        ApplicationModule.class,
        ViewBuildersModule.class,
        AuthServiceModule.class
})

public interface ApplicationComponent {

    @Component.Builder
    interface Builder {
        @BindsInstance
        Builder application(MyApplication application);
        ApplicationComponent build();
    }
    void inject(MyApplication app);
}
  1. Should I annotate OkHttpClient provider as singleton too? (I need Retrofit instance to be singleton in application scope).
  2. What is the difference between provide GsonConverterFactory VS create GsonConverterFactory in retrofit provider like:

    @Module
    public class NetworkModule {
    
        @Provides
        @Singleton
        Retrofit provideRetrofit(OkHttpClient okHttpClient, GsonConverterFactory gsonConverterFactory) {
            return new Retrofit.Builder()
                .addConverterFactory(GsonConverterFactory.create())
                .baseUrl("https://example.com")
                .client(okHttpClient)
                .addCallAdapterFactory(RxJava2CallAdapterFactory.create())
                .build();
        }
    
        @Singleton
        @Provides
        OkHttpClient provideOkHttpClient() {
            return new OkHttpClient.Builder().build();
        }
    }
    

Upvotes: 1

Views: 130

Answers (1)

Vishal Arora
Vishal Arora

Reputation: 2564

1. Should I annotate OkHttpClient provider as singleton too? (I need Retrofit instance to be singleton in application scope).

Marking okhttpclient binding as a singleton will ensure that whichever binding in your system requires an okhttpclient binding will receive the same instance of binding. If you do not mark it as singleton, then every binding which depends on okhttpclient will get a separate instance of okhttpclient. For example, lets assume you create a RestClient which also requires okhttpclient.

@Singleton
@Provides
RestClient provideRestClient(OkHttpClient okhttpclient) {
    return RestClient(okhttpClient);
}

@Provides
@Singleton
Retrofit provideRetrofit(OkHttpClient okHttpClient, GsonConverterFactory gsonConverterFactory) {
    return new Retrofit.Builder()
        .addConverterFactory(GsonConverterFactory.create())
        .baseUrl("https://example.com")
        .client(okHttpClient)
        .addCallAdapterFactory(RxJava2CallAdapterFactory.create())
        .build();
}

@Singleton // marked as singleton
@Provides
OkHttpClient provideOkHttpClient() {
    return new OkHttpClient.Builder().build();
}

In the above case both retrofit and restclient will receive the same instance of Okhttpclient.

But if you remove the @Singleton annotation from okhttpclient binding then both restclient and retrofit will receive different instances of okhttpclient binding.

2. What is the difference between provide GsonConverterFactory VS create GsonConverterFactory in retrofit provider like

Again the difference is visible in terms of scope of binding that you want in your graph. provide GsonConverterFactory will appear as a binding for everyone in your graph whereas reate GsonConverterFactory won't.

Upvotes: 1

Related Questions