Dean Djermanović
07/13/2021, 7:46 AMHttpClient
instance using Koin. Here’s my 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:
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
Dean Djermanović
07/13/2021, 7:52 AMintercept
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.Jemo
07/13/2021, 8:04 AMinline fun <reified T : Any> inject(): T {
return GlobalContext.get().get()
}
val refresher: TokenRefresher = inject()
newAccessToken = refresher.refreshToken()
``````Dean Djermanović
07/13/2021, 8:14 AMGlobalContext
in the shared
module?Jemo
07/13/2021, 8:26 AMJemo
07/13/2021, 8:27 AMJemo
07/13/2021, 8:27 AMexpect object Injector {
inline fun <reified T : Any> inject(): T
}
Dean Djermanović
07/13/2021, 8:39 AMJemo
07/13/2021, 9:03 AM