Which scope should be used to launch coroutines in kotlin multiplatform project? It's a class that generates some data for being displayed in compose desktop composable. I tryed with this:
Copy code
fun longJobFunction(): MutableList<BigData>{
var bigDataList = mutableStateListOf<BigData>()
CoroutineScope(Dispatchers.Main).launch {
withContext(<http://Dispatchers.IO|Dispatchers.IO>) {
//fill bigDataList with big data
Thread.sleep(5000)
}
}
return bigDataList
}
But it gives this exception
java.lang.IllegalStateException: Module with the Main dispatcher is missing. Add dependency providing the Main dispatcher, e.g. 'kotlinx-coroutines-android' and ensure it has the same version as 'kotlinx-coroutines-core'
// hack to allow use of MainScope() in shared code used by JVM console app
implementation("org.jetbrains.kotlinx:kotlinx-coroutines-swing:${Versions.kotlinCoroutines}")
p
Pablo
12/08/2022, 10:42 AM
But should I launch the coroutine that way? should I use MainScope and use tricks? is not a better way?
Pablo
12/08/2022, 10:44 AM
I mean... maybe Dispatchers.Main or CoroutineScope() are not the better ways to launch a coroutine in this scenario, I wonder if there are better ways