Hi everyone, I'm currently stuck in a "simple" use...
# android
t
Hi everyone, I'm currently stuck in a "simple" use case using Hilt: I want a Retrofit service singleton, which takes an OkHttpClient. The OkHttpClient has an interceptor, which uses the Retrofit service singleton (to make a certain request inside the interceptor) The problem is: -> The OkHttpClient takes a Retrofit as argument -> The Retrofit takes an OkHttpClient as argument Cheers
l
You can inject RetrofitService into Interceptor via Provider wrapper:
Copy code
@Singleton
class MyService @Inject constructor(
    private val myOkHttpClient: MyOkHttpClient,
)

@Singleton
class MyOkHttpClient @Inject constructor(
    private val myInterceptor: MyInterceptor,
)

class MyInterceptor @Inject constructor(
    private val myService: Provider<MyService>,
) : Interceptor {

    override fun intercept(chain: Interceptor.Chain): Response {
        myService.get()
        return chain.proceed(chain.request())
    }
}
This avoids circular dependency.
👍 1
🙌 2
t
Oh wow! That's it! Thanks so much!
👍 1