问题描述
我正在使用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)
}
}
注意:当我一次仅下载1个文件时,一切正常。对于错误,会立即生成失败通知,并且流程会立即捕获到阻塞。
但是,当我同时下载多个文件时,只要切换网络,就会调用onError,因此,当切换网络时,将调用所有Worker的onError(这是必需的行为)。问题如下:
案例1:有时,任何Worker
都不会立即调用单个catch块。
案例2:有时,一个工人的任意一个捕获块被立即调用,但是对于其他工人,这需要花费大量时间。
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)