【发布时间】:2020-01-07 15:39:09
【问题描述】:
我刚开始使用协程进行 2 个并行运行的异步调用。 aync 任务之一进行服务调用。此服务有时可能需要很长时间才能响应。在这种情况下,我的代码在我的匕首注入拦截器函数中崩溃。我试图捕捉错误并将其扔回我的协程,但它从未被捕捉到。
协程:
try {
CoroutineScope(Dispatchers.IO).launch {
val deferredList = listOf(async {
myAPI?.getAllMarks(WebService.getAwsAccessToken(),
UserInfoManager?.userInfo?.guid)
}, async {
getClassFromJsonFile(R.raw.temp_whatsnew, WhatsNew::class.java)
})
var theList: List<Any> =
deferredList.awaitAll() // wait for all data to be processed without blocking the UI thread
withContext(Dispatchers.Main) {
mListener?.onWhatsNewDownloaded(theList.get(1) as WhatsNew,
theList.get(0) as List<Coachmark>)
}
}
} catch (t: Throwable) {
//exception never reaches here!
Log.v(WhatsNewInteractorImpl::class.java.simpleName, t.localizedMessage)
}
拦截器:
try {
response = chain.proceed(newRequest);
} catch (SocketTimeoutException e) {
//CRASHES HERE!
throw new SocketTimeoutException("socket timeout");
}
【问题讨论】:
标签: android coroutine socket-timeout-exception