ribesg
09/25/2020, 10:25 AMhttp.get
will freeze the coroutine it runs in and I’m trying to prevent it from propagating outside coroutineScope { }
but my entire codebase still gets frozen...
override suspend fun getUsers(): List<User> {
val http = http.freeze()
val url = "$BASE_URL$USERS_PATH".freeze()
val result =
coroutineScope {
http.get<List<User>>(url)
}
return result
}
How do you properly isolate such call? I’ve been trying random things for daysbezrukov
09/25/2020, 10:51 AMcoroutineScope
is designed for parallel decomposition, calling single function from it is meaningless. If you want to switch thread, you need to use withContext(<http://Dispatchers.IO|Dispatchers.IO>)
bezrukov
09/25/2020, 10:53 AMoverride suspend fun getUsers(): List<User> = withContext(<http://Dispatchers.IO|Dispatchers.IO>) {
val http = http.freeze()
val url = "$BASE_URL$USERS_PATH".freeze()
return http.get<List<User>>(url)
}
louiscad
09/25/2020, 10:55 AM<http://Dispatchers.IO|Dispatchers.IO>
is JVM-only. Here, it's about Kotlin/Native, ktor and coroutines freezing.bezrukov
09/25/2020, 10:56 AMribesg
09/25/2020, 11:01 AMlouiscad
09/25/2020, 11:02 AMribesg
09/25/2020, 11:07 AMribesg
09/25/2020, 11:22 AMelizarov
09/25/2020, 12:04 PMGlobalScope.launch
) which will allow to limit freezing to that detached part.louiscad
09/25/2020, 12:05 PMGlobalScope.launch(Dispatchers.Main)
+ join()
could do?louiscad
09/25/2020, 12:06 PMlouiscad
09/25/2020, 12:55 PMsuspend inline fun <R> runIsolated(noinline block: suspend CoroutineScope.() -> R): R {
val coroutine = GlobalScope.async(Dispatchers.Main, block = block)
try {
return coroutine.await()
} catch (e: CancellationException) {
coroutine.cancel()
throw e
}
}
ribesg
09/25/2020, 1:36 PMCoroutineExceptionHandler
being frozen, not the coroutine suspend lambda. Not sure it should be frozen, but as long as the suspend lambda isn’t frozen (and it isn’t) I can mostly replace the CEH with a try catch.