小编Wah*_*han的帖子

如何解决 Dagger (Hilt) 中的循环依赖

我想使用拦截器刷新我的令牌,但我的拦截器需要 API 服务来进行 API 调用。我陷入了依赖循环。

这是我的 ApplicationModule 类:

@Module
@InstallIn(ApplicationComponent::class)
class ApplicationModule {


    @Provides
    fun providerBaseUrl() = AppConstants.BASE_URL

    @Provides
    @Singleton
    fun provideOkHttpClient(authInterceptor: AuthInterceptor,
                            networkInterceptor: NetworkInterceptor) = if (BuildConfig.DEBUG) {

        val loggingInterceptor = HttpLoggingInterceptor()
        loggingInterceptor.setLevel(HttpLoggingInterceptor.Level.BODY)
        OkHttpClient.Builder()
                .addInterceptor(authInterceptor)
                .addInterceptor(loggingInterceptor)
                .addInterceptor(networkInterceptor)
                //   .addInterceptor(refreshTokenInterceptor) // I want to put my interceptor here
                .connectTimeout(2, TimeUnit.MINUTES)
                .readTimeout(2, TimeUnit.MINUTES)
                .writeTimeout(2, TimeUnit.MINUTES)

                .build()
    } else OkHttpClient
            .Builder()
            .connectTimeout(2, TimeUnit.MINUTES)
            .readTimeout(2, TimeUnit.MINUTES)
            .writeTimeout(2, TimeUnit.MINUTES)
            .addInterceptor(authInterceptor)
            .addInterceptor(networkInterceptor)
            .build()


    @Provides
    @Singleton
    fun provideRetrofit(
            okHttpClient: OkHttpClient,
            BASE_URL: String
    ): Retrofit =
            Retrofit.Builder() …
Run Code Online (Sandbox Code Playgroud)

android kotlin dagger retrofit dagger-hilt

6
推荐指数
2
解决办法
2630
查看次数

标签 统计

android ×1

dagger ×1

dagger-hilt ×1

kotlin ×1

retrofit ×1