Reputation: 3924
I have a situation where I need two Retrofit Services, each one has its business implementation.
@Provides
@Singleton
@Named("defaultMulhimService")
MulhimService provideMulhimService() {
return MulhimService.Creator.newMulhimService();
}
@Provides
@Singleton
@Named("MulhimServiceWithCache")
MulhimService providesMulhimServiceWithCache(){
return MulhimService.Creator.newMulhimServiceWithCache(mApplication);
}
I'm already looked at this answer which suggest using @Named annotation to differ multiple instances at modules, but what I don't know, how to inject them.
Upvotes: 1
Views: 1859
Reputation: 2014
You could use something like this (https://guides.codepath.com/android/Dependency-Injection-with-Dagger-2) -
@Provides @Named("cached")
@Singleton
OkHttpClient provideOkHttpClient(Cache cache) {
OkHttpClient client = new OkHttpClient();
client.setCache(cache);
return client;
}
@Provides @Named("non_cached") @Singleton
OkHttpClient provideOkHttpClient() {
OkHttpClient client = new OkHttpClient();
return client;
}
@Inject @Named("cached") OkHttpClient client;
@Inject @Named("non_cached") OkHttpClient client2;
Basically you inject the instance using the @Named qualifier
Upvotes: 5