Android 可取消延续不';我不能马上恢复
我正在使用Android 可取消延续不';我不能马上恢复,android,asynchronous,android-workmanager,continuations,Android,Asynchronous,Android Workmanager,Continuations,我正在使用CoroutineWorker做一些下载工作 我的代码如下: class VideoDownloader(private val appContext: Context, params: WorkerParameters) : CoroutineWorker(appContext, params) { override suspend fun doWork(): Result { try { suspendCancellableCoroutine
CoroutineWorker
做一些下载工作
我的代码如下:
class VideoDownloader(private val appContext: Context, params: WorkerParameters) :
CoroutineWorker(appContext, params) {
override suspend fun doWork(): Result {
try {
suspendCancellableCoroutine<Int> { cancellableContinuation ->
PRDownloader
.download(downloadUrl, filePath, name)
.build()
.setOnStartOrResumeListener {}
.start(object : OnDownloadListener {
override fun onDownloadComplete() {}
override fun onError(error: Error?) {
updateProgressInDb(videoInfo)
showFinalNotification("Download failed", "title")
cancellableContinuation.resumeWithException(
error?.connectionException ?: Throwable()
)
}
})
}
}catch (e: Exception) {
return Result.failure()
}
return Result.success()
}
private fun updateProgressInDb(videoInfo: VideoInfo) =
CoroutineScope(Dispatchers.IO).launch {
repository.updateVideoProgress(videoInfo)
}
}
class VideoDownloader(私有val-appContext:Context,参数:WorkerParameters):
CoroutineWorker(应用上下文,参数){
覆盖暂停工作():结果{
试一试{
SuspendCancelableCoroutine{CancelableContinuation->
下载程序
.download(下载URL、文件路径、名称)
.build()
.setOnStartOrResumeListener{}
.start(对象:OnDownloadListener{
重写onDownloadComplete(){}
覆盖错误(错误:错误?){
updateProgressInDb(视频信息)
showFinalNotification(“下载失败”、“标题”)
CancelableContinuation.resumeWithException(
错误?.connectionException?:Throwable()
)
}
})
}
}捕获(e:例外){
返回Result.failure()
}
返回Result.success()
}
私人娱乐更新程序NDB(videoInfo:videoInfo)=
协同路由示波器(Dispatchers.IO)。启动{
repository.updateVideoProgress(videoInfo)
}
}
注意:当我一次只下载一个文件时,一切正常。对于错误,立即生成失败通知,流立即到达catch块
但是,当我同时下载多个文件时,每当网络切换时都会调用onError,因此当网络切换时,会调用所有worker的onError(这是必需的行为)。问题如下:
案例1:有时,任何工作者都不会立即调用一个catch块
案例2:有时,一个工人中的任何一个的1个捕捉块会立即被调用,但对于其他工人来说,这需要花费大量的时间