I'm having a weird issue with multithreaded corout...
# kotlin-native
a
I'm having a weird issue with multithreaded coroutines + sqldelight + ktor. Here's approximate code:
Copy code
...
mainScope.launch {
    val dataFromServer = ktorClient.callFirstApi() // runs on main thread - success
    sqlDelightClient.storeData(dataFromServer) // runs on background using withContext(workerDispatcher) - success
    val otherDataFromServer = ktorClient.callSecondApi() // runs on main thread - fails with JsonDecodingException: Invalid JSON at 0: Expected '{, kind: CLASS'
}
I log request details and if I replay the request in Postman I get valid response. If I don't use
withContext(workerDispatcher)
to store data to database everything works fine and second API is successful. Note: I run API on the main thread because Ktor doesn't support background threads yet (should be added in 1.3.0).
k
mt coroutines version will conflict with the versions from ktor and sqldelight extensions. Need to make sure you’re suppressing those other versions. There’s a ktor issue (probably issues) when you force it to use mt coroutines which I’m looking at today
a
I had issues with mt coroutines conflicting with Ktor a few days ago, but that's been resolved, and didn't have to change anything for sqldelight.
./gradlew dependencies
only shows one version of coroutines being used - 1.3.2-native-mt-SNAPSHOT
k
you say you're using
withContext
for the second call, but is it blocking the main thread? otherwise the 3rd call will happen before the second completes.
otherwise you'll need
runBlocking
a
it all runs within a coroutine
mainScope.launch {...}
so all calls are executed sequentially