Hiya 👋, I’m still trying to grasp coroutines and dispatchers, and I’ve encountered an issue in my Android app where the thread seemed to be blocked, which I’ve managed to fix but don’t quite understand why 😕 Any explanation would be appreciated!
In
onResume
of my main fragment, I’ve got code like this (further down the chain, I don’t call this directly like this in
onResume
):
suspend fun onSessionStart() = withContext(Dispatcher.javascript) {
for(feature in featuresList) {
feature.setUpSynchronously()
myActivity.lifecycleScope.launch(Dispatcher.javascript) {
feature.setUpAsync()
}
}
}
Right after I call this method above, I call this:
suspend fun start() = withContext(Dispatcher.javascript) {
//...
}
Note that
setUpAsync()
may call a method which switches to
Dispatcher.network
, but doesn’t launch a new coroutine.
My problem is that on a fresh app launch, this code runs fine, but when I trigger
onSessionStart
again, when the app is still alive, I never get to
start()
and the app stalls. I can see by the logs that all of the
setUpAsync()
calls finish, but they don’t seem to return 🤷🏻♀️
Removing the context when launching the async set up, seems to fix the problem:
myActivity.lifecycleScope.launch {
feature.setUpAsync()
}
But I don’t understand why, any idea?