Colton Idle
12/15/2019, 7:58 PMviewLifecycleOwner.lifecycleScope.launch {
val work1 = async { getThingsDone(43) }
val work2 = async { getThingsDoneAgain(123) }
if (work1.await().isSuccessful && work2.await().isSuccessful) {
//do my thing
} else {
//failure message
}
Basically, the getThingsDone calls are retrofit network calls. If one of them is still going on and I go into airplane mode, I get a crash.
What have I tried?
1.
try {
viewLifecycleOwner.lifecycleScope.launch {
val work1 = async { getThingsDone(43) }
val work2 = async { getThingsDoneAgain(123) }
if (work1.await().isSuccessful && work2.await().isSuccessful) {
//do my thing
} else {
//failure message
}
} catch (e: Exception) {
//show failure message
}
2.
viewLifecycleOwner.lifecycleScope.launch {
try {
val work1 = async { getThingsDone(43) }
val work2 = async { getThingsDoneAgain(123) }
if (work1.await().isSuccessful && work2.await().isSuccessful) {
//do my thing
} else {
//failure message
}
} catch (e: Exception) {
//show failure message
}
}
3.
viewLifecycleOwner.lifecycleScope.launch {
val work1 : Deferred<Response<MyType>>
val work2: Deferred<Response<MyType>>
try {
work1 = async { getThingsDone(43) }
work2 = async { getThingsDoneAgain(123) }
} catch (e: Exception) {
//show failure message
}
if (work1.await().isSuccessful && work2.await().isSuccessful) {
//do my thing
} else {
//failure message
}
}
4.
viewLifecycleOwner.lifecycleScope.launch {
val work1 = async {
try {
getThingsDone(43)
} catch (e: Exception) {
//show failure message
}
}
val work2 = async {
try {
getThingsDoneAgain(123)
} catch (e: Exception) {
//show failure message
}
}
if (work1.await().isSuccessful && work2.await().isSuccessful) {
//do my thing
} else {
//failure message
}
}
I would really appreciate some help. As of now this works super nice because I essentially have two async network calls, and my code ONLY continues if both of them complete! But in the off chance that I have a network error (or force one by going into airplane mode during the request), my Android app dies.streetsofboston
12/15/2019, 8:10 PMColton Idle
12/15/2019, 8:18 PMstreetsofboston
12/15/2019, 8:22 PMval ceh = CoroutineExceptionHandler { c, t -> ..... handle the exception here ... }
viewLifecycleOwner.lifecycleScope.launch(ceh) { ...
...
}
Colton Idle
12/15/2019, 8:29 PMtrathschlag
12/16/2019, 8:24 AMviewLifecycleOwner.lifecycleScope.launch {
try {
coroutineScope {
// all your async stuff goes here
}
} catch (e: Exception) {
// handle error
}
}
But actually looking at your code I think listing 4
should work, too. So maybe there is another problem we cannot see here.Pablichjenkov
12/16/2019, 4:06 PMasync
causes its siblings and parent death. Try to avoid this as much as possible, if it is a known exception obvious.Colton Idle
12/23/2019, 12:03 AMPablichjenkov
12/23/2019, 12:47 AM