Hello coroutine community :wave: I’ve just update...
# coroutines
f
Hello coroutine community 👋 I’ve just updated my kotlin version to
1.6.20
and I get a crash when calling a
suspendCoroutine
function :
Copy code
var topupCountries: Deferred<Set<String>> = buildCountriesAsync()
    private set

private fun buildCountriesAsync() = CoroutineScope(backgroundDispatcher).async(start = CoroutineStart.LAZY) {
    getCountries()
}



private suspend fun getCountries(): Set<String> {
    return try {
        topupCountries.getCompleted()
    } catch (exception: IllegalStateException) {
    
        // Crash here
        fetchRestrictedTopupCountries()
    } }

private suspend fun fetchRestrictedTopupCountries(): Set<String> {
    return suspendCoroutine { continuation ->
        val request = ApiTopupRequestFactory.getTopupCountries(
            listener = {
                val setResult = it.countries.toSet()
                continuation.resume(setResult)
            },
            errorListener = {
                continuation.resume(setOf())
            }
        )
        execute(request)
    }
}
The error is :
java.lang.ClassCastException: kotlin.coroutines.intrinsics.CoroutineSingletons cannot be cast to java.util.Set
Am I doing something wrong here ? Anyone else with this issue ?
After some research, I found a similar problem back in 2019 : https://github.com/Kotlin/kotlinx.coroutines/issues/1091
It seems the issue comes from the
inline
and
crossinline
usage
Indeed the
suspendCoroutine
uses thoses two :
suspend inline fun <T> suspendCoroutine(
crossinline block: (Continuation<T>) -> Unit
): T
Just read that the
getCompleted
function is not meant to be used like that