Home > Blockchain >  How to create multiple instances of an object using dagger in android
How to create multiple instances of an object using dagger in android

Time:12-31

I use dagger to create instances for application

But due to some requirements, i need multiple instances of same object,

Can any one suggest good way of doing that ?

CodePudding user response:

You can use @Named annotation to achieve this behaviour,

Example:

Step 1: Declare your method with @Named annotation with unique key

@Provides
@Singleton
@Named("cached")
fun someInstance() = return someInstance()

Step 2: Use the instance with the declared key

@Inject @Named("cached")
val instance: someInstance

CodePudding user response:

You can use @Named("") to create multiple instances. Here I create 2 instances of Interceptor with different @Named("")

Java:

@InstallIn(SingletonComponent.class)
@Module
class NetworkModule {
    @Provides
    @Singleton
    @Named("headerInterceptor")
    private Interceptor provideInterceptor(UserManager userManager) {
        return new Interceptor() {
            @NonNull
            @Override
            public Response intercept(@NonNull Chain chain) throws IOException {
                return chain.proceed(chain.request());
            }
        };
    }

    @Provides
    @Singleton
    @Named("loggingInterceptor")
    private Interceptor provideLoggingInterceptor() {
        return new LoggingInterceptor.Builder()
                .loggable(BuildConfig.DEBUG)
                .setLevel(Level.BODY)
                .log(Platform.INFO)
                .request("Request")
                .response("Response")
                .build();
    }

    @Provides
    @Singleton
    private OkHttpClient provideOkHttpClient(
            Cache cache,
            @Named("headerInterceptor") Interceptor interceptor,
            @Named("loggingInterceptor") Interceptor loggingInterceptor
    ) {
        return new OkHttpClient.Builder()
                .cache(cache)
                .connectTimeout(60, TimeUnit.SECONDS)
                .readTimeout(60, TimeUnit.SECONDS)
                .writeTimeout(60, TimeUnit.SECONDS)
                .addInterceptor(interceptor)
                .addInterceptor(loggingInterceptor)
                .build();
    }
}

Kotlin

@InstallIn(SingletonComponent::class)
@Module
class NetworkModule {
    @Provides
    @Singleton
    @Named("headerInterceptor")
    fun provideInterceptor(userManager: UserManager): Interceptor {
        return Interceptor { chain ->
            chain.proceed(chain.request())
        }
    }

    @Provides
    @Singleton
    @Named("loggingInterceptor")
    fun provideLoggingInterceptor(): Interceptor {
        return LoggingInterceptor.Builder()
            .loggable(BuildConfig.DEBUG)
            .setLevel(Level.BODY)
            .log(Platform.INFO)
            .request("Request")
            .response("Response")
            .build()
    }

    @Provides
    @Singleton
    fun provideOkHttpClient(
        cache: Cache,
        @Named("headerInterceptor") interceptor: Interceptor,
        @Named("loggingInterceptor") loggingInterceptor: Interceptor
    ): OkHttpClient {
        return OkHttpClient.Builder()
            .cache(cache)
            .connectTimeout(60, TimeUnit.SECONDS)
            .readTimeout(60, TimeUnit.SECONDS)
            .writeTimeout(60, TimeUnit.SECONDS)
            .addInterceptor(interceptor)
            .addInterceptor(loggingInterceptor)
            .build()
    }
}
  • Related