I have an issue when I try to provide Ktor’s `Http...
# multiplatform
d
I have an issue when I try to provide Ktor’s
HttpClient
instance using Koin. Here’s my code:
Copy code
single {
        val buildVariant: BuildVariant = get()
        val httpClientEngine: HttpClientEngine = get()
        val kotlinxSerializer: KotlinxSerializer = get()
        val tokenRefresher: TokenRefresher = get()

        HttpClient(httpClientEngine) {
            install(JsonFeature) {
                serializer = kotlinxSerializer
            }

            install(Logging) {
                ...
            }

            install(HttpSend) {
                intercept { clientCall, requestBuilder ->
                    ...

                    requestBuilder.takeFrom(clientCall.request)

                    val newAccessToken = tokenRefresher.refreshToken()
                    requestBuilder.setAuthorizationHeader(newAccessToken)
                    execute(requestBuilder)
                }
            }
        }
    }
The problem is in this line:
Copy code
val newAccessToken = tokenRefresher.refreshToken()
I get
kotlin.native.concurrent.InvalidMutabilityException: mutation attempt of frozen org.koin.core.scope.Scope
Can someone please explain why do I get this exception if I’m not trying to modify
tokenRefresher
?
refreshToken
is a suspending function, but for the debugging purposes, I made it a regular function which returns some string constant. The behavior is the same, I still get the same exception. I’m using Ktor version
1.5.2
Here’s my understanding so far.
intercept
lambda switches threads internally, freezes
tokenRefresher
and therefore the whole
Scope
. But since I don’t try to mutate
tokenRefresher
, I simply call a function on it, I don’t understand why the exception gets thrown.
j
Try this
Copy code
inline fun <reified T : Any> inject(): T {
    return GlobalContext.get().get()
}
val refresher: TokenRefresher = inject()
Copy code
newAccessToken = refresher.refreshToken()
``````
d
Can I access
GlobalContext
in the
shared
module?
j
Yes
Sorry not directly
I have expect class
Copy code
expect object Injector {
    inline fun <reified T : Any> inject(): T
}
d
@Jemo Yes, this works. Can you please explain what was the problem and why this works?