У меня есть этот код, мой viewmodel init < /p>
val scope = CoroutineScope(Dispatchers.Main )
var child1:Job = Job()
var child2 : Deferred = CompletableDeferred(Unit)
child1 =scope.launch {
try {
child2 = this.async {
for(i in 1..5){
if(i!=3){
println(" i= $i")
}else{
throw RuntimeException("Oops")
}
}
}
child2.invokeOnCompletion { cause ->
println("invokeOnCompletion")
when {
cause == null ->
println("Completed normally — cause is null")
cause is CancellationException ->
println("Cancelled: $cause")
else ->
println("Failed with exception: $cause")
}
}
child2.await()
}catch (e:Exception){
println("catch ${e.message}")
}
}
println("child1 is parent of child2 ${child1.children.contains(child2)}")
println("scope is parent of child1 ${scope.coroutineContext[Job]?.children?.contains(child1)}")
< /code>
и выход напечатан I для значений 1 и двух, затем < /p>
Failed with exception: java.lang.RuntimeException: Oops
catch Oops
< /code>
Хотя уловка называется приложением разбито.>
Подробнее здесь: https://stackoverflow.com/questions/796 ... in-corutin
Обработка ошибок и структурированная параллелизм в Corutin ⇐ Android
-
- Похожие темы
- Ответы
- Просмотры
- Последнее сообщение