Hiya :wave:, I’m still trying to grasp coroutines ...
# android
m
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
):
Copy code
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:
Copy code
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:
Copy code
myActivity.lifecycleScope.launch {
                feature.setUpAsync()
            }
But I don’t understand why, any idea?
a
Hey @Martyna Maron, I've never seen
Dispatcher.javascript
before - is this a custom dispatcher in your project?
m
yeah, same for the network one 👍 just maps to a custom
ExecutorService
🙏 1
a
m
Don’t think I have, thanks!
c
@Martyna Maron you might also have more luck asking this question in #coroutines
g
What kind custom execution service?
Also how do you call this onSessionStart from onResume?
Also setUpSynchronously looks quite suspicious, like some blocking method
n
the most suspicious part is
Dispatchers.javascript
, launch should return immediately, even may before
setUpAsync
called, i guess the thread underline
javascript
is blocked