Allan Wang
09/17/2018, 10:00 PMlaunch
blocks?
I have a method created like so:
suspend fun <T> Deferred<T>.awaitOrRedirect(context: Context): T? =
try {
await()
} catch (e: Exception) {
logd("Error, redirect to Login")
LoginActivity.launch(context)
null
}
But the errors seem to just pass through and cancel the coroutines altogether without launching the activity.
To give more details, I’m building for Android, and I’m using CoroutineScrope
within the activity that is calling the method. I’m using launch
as it is used for most of the example code, and it looks like the only difference with async
is that async
cancels the parent and returns a deferred value. I also see that there are `CoroutineExceptionHandler`s, though I’m not sure if that is the right fit here.gildor
09/17/2018, 11:26 PMAllan Wang
09/18/2018, 1:19 AMgildor
09/18/2018, 1:20 AMcoroutine implementation for retrofitwhich one?
the app stops as a wholedo you mean sometimes app crashes? Are you sure that this method is the reason?
await()
?
If so, I would say that it’s wrong approach. Much better to use custom okhttp Interceptor that does this for youAllan Wang
09/18/2018, 1:30 AMgildor
09/18/2018, 1:32 AMbut it doesn’t have access to the context necessary to launch the login activityJust pass application context to Interceptor’s constructor, no problems there, just start activity in a new task (you cannot start it in the same task without activity context) Another approach: interceptor just send some event that can be caught by current active activity and show login screen on the same task
Allan Wang
09/18/2018, 1:34 AM