Greg Stepniewski
09/16/2019, 11:34 AMsuspend fun loadTwoThings() = coroutineScope {
val job1 = async { getDataOne() }
val job2 = async { getDataTwo() }
saveData(job1.await(), job2.await())
}
different from this
suspend fun loadTwoThings() = {
val job1 = async { getDataOne() }
val job2 = async { getDataTwo() }
saveData(job1.await(), job2.await())
}
As I understand, if I cancel a coroutine that called loadTwoThings
, without coroutineScope the two async will keep running. Also, if one async fails it will let the other keep running.
Why is that? A failed child is supposed to cancel its parent, and a cancelled parent is supposed to cancel its children. Are the two asyncs not children of the main coroutine? What exactly does coroutineScope
do that makes a difference?Dominaezzz
09/16/2019, 11:36 AMCoroutineScope
is basically a way to implicitly/easily pass down context to child coroutines.Dominaezzz
09/16/2019, 11:37 AMasync
coroutine builder to establish a parent/child relationship.Greg Stepniewski
09/16/2019, 11:38 AMDominaezzz
09/16/2019, 11:39 AMDominaezzz
09/16/2019, 11:39 AMGreg Stepniewski
09/16/2019, 11:41 AMDominaezzz
09/16/2019, 11:41 AMGreg Stepniewski
09/16/2019, 11:43 AMDominaezzz
09/16/2019, 11:47 AMGreg Stepniewski
09/16/2019, 11:47 AMGreg Stepniewski
09/16/2019, 11:47 AMDominaezzz
09/16/2019, 11:47 AMCoroutineScope
directly.Greg Stepniewski
09/16/2019, 11:48 AMGreg Stepniewski
09/16/2019, 11:49 AMDominaezzz
09/16/2019, 11:49 AMDominaezzz
09/16/2019, 11:50 AMviewModelScope
variable you can use.Greg Stepniewski
09/16/2019, 12:02 PMDominaezzz
09/16/2019, 1:23 PMDominaezzz
09/16/2019, 1:23 PM