Using coroutineScope in a suspend function that will be called from a CoroutineScope, is it giving the same result as passing that CoroutineScope as a parameter to the suspend function ?
import kotlinx.coroutines.coroutineScope
suspend fun loadContributorsConcurrent() {
coroutineScope {
// launch...
}
}
suspend fun loadContributorsConcurrent(outerScope: CoroutineScope) {
outerScope.run {
// launch...
}
}
No. In the second version, you can pass a scope that does not match the scope in which
loadContributorsConcurrentis actually run. The use ofrunmakes things even worse, as code will usually be run in the correct scope except launch or async coroutines.The first pattern is the correct one.