mingkangpan
03/06/2020, 9:01 AMval upperJob = launch {
val jobFoo1 = launch {
foo1()
}
val jobFoo2 = launch {
foo2()
}
}
upperJob.cancel() //won't cancel jobFoo1 and jobFoo2
right?
I have to use async
hereKirill Prybylsky
03/06/2020, 9:06 AMmingkangpan
03/06/2020, 9:13 AMKirill Prybylsky
03/06/2020, 9:15 AMmingkangpan
03/06/2020, 9:17 AMKirill Prybylsky
03/06/2020, 9:19 AMmingkangpan
03/06/2020, 9:36 AMKirill Prybylsky
03/06/2020, 9:36 AMmingkangpan
03/06/2020, 9:38 AMlaunch {
try{
supervisorScope {
// first api
launch { }
//second api
launch { }
}
} cathc(e : Exception) {
}
}
launch {
try{
supervisorScope {
// first api
async { }
//second api
async { }
}
} cathc(e : Exception) {
}
}
Kirill Prybylsky
03/06/2020, 9:41 AMmingkangpan
03/06/2020, 9:41 AMKirill Prybylsky
03/06/2020, 9:42 AMmingkangpan
03/06/2020, 9:43 AMoverride fun onChanged(t: AccountInfo) {
fetchDataAfterAccountInfo?.cancel()
fetchDataAfterAccountInfo = viewModelScope.launch {
try {
supervisorScope {
launch {
val purchases = appService.fetchPurchasesWithPickupData().purchases
pickPurchasesLiveData.postValue(purchases)
}
launch {
val paymentPlan = appService.fetchPaymentPlan()
paymentPlanLiveData.postValue(paymentPlan.instalments)
}
}
} catch (e : Exception) {
Timber.e(e, "loading data after accountInfo failed")
}
}
}
Kirill Prybylsky
03/06/2020, 9:44 AMmingkangpan
03/06/2020, 9:46 AMKirill Prybylsky
03/06/2020, 9:48 AMmingkangpan
03/06/2020, 9:49 AMKirill Prybylsky
03/06/2020, 9:49 AMmingkangpan
03/06/2020, 9:50 AMliveData.posValue
?Kirill Prybylsky
03/06/2020, 9:50 AMmingkangpan
03/06/2020, 9:53 AMKirill Prybylsky
03/06/2020, 9:55 AMmingkangpan
03/06/2020, 9:56 AMKirill Prybylsky
03/06/2020, 9:56 AMmingkangpan
03/06/2020, 10:13 AMfun main() = runBlocking {
try {
supervisorScope {
launch { foo1() }
launch { foo2() }
delay(1000)
println("Waited 1000ms")
}
} catch (e : Exception) {
println(e)
}
}
suspend fun foo1() {
delay(300)
throw RuntimeException("Something went wrong")
}
suspend fun foo2() : Int{
delay(500)
println("returning 5")
return 5
}
Exception in thread "main" java.lang.RuntimeException: Something went wrong
at com.minki.example.MainKt.foo1(Main.kt:20)
at com.minki.example.MainKt$foo1$1.invokeSuspend(Main.kt)
at kotlin.coroutines.jvm.internal.BaseContinuationImpl.resumeWith(ContinuationImpl.kt:33)
at kotlinx.coroutines.DispatchedTaskKt.resume(DispatchedTask.kt:173)
at kotlinx.coroutines.DispatchedTaskKt.dispatch(DispatchedTask.kt:111)
at kotlinx.coroutines.CancellableContinuationImpl.dispatchResume(CancellableContinuationImpl.kt:305)
at kotlinx.coroutines.CancellableContinuationImpl.resumeImpl(CancellableContinuationImpl.kt:315)
at kotlinx.coroutines.CancellableContinuationImpl.resumeUndispatched(CancellableContinuationImpl.kt:397)
at kotlinx.coroutines.EventLoopImplBase$DelayedResumeTask.run(EventLoop.common.kt:484)
at kotlinx.coroutines.EventLoopImplBase.processNextEvent(EventLoop.common.kt:271)
at kotlinx.coroutines.BlockingCoroutine.joinBlocking(Builders.kt:79)
at kotlinx.coroutines.BuildersKt__BuildersKt.runBlocking(Builders.kt:54)
at kotlinx.coroutines.BuildersKt.runBlocking(Unknown Source)
at kotlinx.coroutines.BuildersKt__BuildersKt.runBlocking$default(Builders.kt:36)
at kotlinx.coroutines.BuildersKt.runBlocking$default(Unknown Source)
at com.minki.example.MainKt.main(Main.kt:5)
at com.minki.example.MainKt.main(Main.kt)
returning 5
Waited 1000ms
Kirill Prybylsky
03/06/2020, 10:16 AMmingkangpan
03/06/2020, 10:18 AMKirill Prybylsky
03/06/2020, 10:27 AMstreetsofboston
03/06/2020, 12:22 PM