Home > Enterprise >  Coroutine launch() while inside another coroutine
Coroutine launch() while inside another coroutine

Time:11-17

I have the following code (pseudocode)

fun onMapReady()
{
    //do some stuff on current thread (main thread)

    //get data from server
    GlobalScope.launch(Dispatchers.IO){

        getDataFromServer { result->

            //update UI on main thread
            launch(Dispatchers.Main){
                updateUI(result) //BREAKPOINT HERE NEVER CALLED
            }
        }

    }
}

As stated there as a comment, the code never enters the coroutine dispatching onto main queue. The below however works if I explicitly use GlobalScope.launch(Dispatchers.Main) instead of just launch(Dispatchers.Main)

fun onMapReady()
{
    //do some stuff on current thread (main thread)

    //get data from server
    GlobalScope.launch(Dispatchers.IO){

        getDataFromServer { result->

            //update UI on main thread
            GlobalScope.launch(Dispatchers.Main){
                updateUI(result) //BREAKPOINT HERE IS CALLED
            }
        }

    }
}

Why does the first approach not work?

CodePudding user response:

I believe the problem here is that getDataFromServer() is asynchronous, it immediately returns and therefore you invoke launch(Dispatchers.Main) after you exited from the GlobalScope.launch(Dispatchers.IO) { ... } block. In other words: you try to start a coroutine using a coroutine scope that has finished already.

My suggestion is to not mix asynchronous, callback-based APIs with coroutines like this. Coroutines work best with suspend functions that are synchronous. Also, if you prefer to execute everything asynchronously and independently of other tasks (your onMapReady() started 3 separate asynchronous operations) then I think coroutines are not at all a good choice.

Speaking about your example: are you sure you can't execute getDataFromServer() from the main thread directly? It shouldn't block the main thread as it is asynchronous. Similarly, in some libraries callbacks are automatically executed in the main thread and in such case your example could be replaced with just:

fun onMapReady() {
    getDataFromServer { result->
        updateUI(result)
    }
}

If the result is executed in a background thread then you can use GlobalScope.launch(Dispatchers.Main) as you did, but this is not really the usual way how we use coroutines. Or you can use utilities like e.g. runOnUiThread() on Android which probably makes more sense.

CodePudding user response:

@broot already explained the gist of the problem. You're trying to launch a coroutine in the child scope of the outer GlobalScope.launch, but that scope is already done when the callback of getDataFromServer is called.

So in short, don't capture the outer scope in a callback that will be called in a place/time that you don't control.

One nicer way to deal with your problem would be to make getDataFromServer suspending instead of callback-based. If it's an API you don't control, you can create a suspending wrapper this way:

suspend fun getDataFromServerSuspend(): ResultType = suspendCoroutine { cont ->
    getDataFromServer { result ->
        cont.resume(result)
    }
}

You can then simplify your calling code:

fun onMapReady() {

    // instead of GlobalScope, please use viewModelScope or lifecycleScope,
    // or something more relevant (see explanation below)
    GlobalScope.launch(Dispatchers.IO) {

        val result = getDataFromServer()

        // you don't need a separate coroutine, just a context switch
        withContext(Dispatchers.Main) {
            updateUI(result)
        }
    }
}

As a side note, GlobalScope is probably not what you want, here. You should instead use a scope that maps to the lifecycle of your view or view model (viewModelScope or lifecycleScope) because you're not interested in the result of this coroutine if the view is destroyed (so it should just be cancelled). This will avoid coroutine leaks if for some reason something hangs or loops inside the coroutine.

  • Related