如何每5秒重试一次WorkManager的工作 [英] How to run WorkManager's work retry every 5 seconds
问题描述
如何每5秒重试一次? 如果成功了就取消它吗?
How to retry work with every 5 seconds? And if was successful then cancel it?
下面的解决方案每10秒运行一次+时间线性增长.
With the solution below it runs every 10 seconds + Linear growth of time.
// todo: schedule, and invoke worker every 5 seconds
// todo: if the work is done and there is no more work in queue - cancel worker.
fun scheduleBatchUpload(uniqueWorkName: String) {
val logBuilder = PeriodicWorkRequest.Builder(StreamLogWorker::class.java, 5, TimeUnit.SECONDS)
logBuilder.setBackoffCriteria(BackoffPolicy.LINEAR, 5000, TimeUnit.MILLISECONDS) // Custom retry not working
WorkManager.getInstance().enqueueUniquePeriodicWork(uniqueWorkName, ExistingPeriodicWorkPolicy.REPLACE, logBuilder.build())
}
class StreamLogWorker(context: Context, workerParams: WorkerParameters) : Worker(context, workerParams) {
override fun doWork(): Result {
Log.e("!!!!!!!!!!", "doWork")
return Result.retry()
}
}
推荐答案
对于PeriodicWorkRequest
,这是不可能的.如果您查看PeriodicWorkRequest.Builder
构造函数的文档正在使用中,您会看到它显示有关第二个参数的信息
That is not possible with the PeriodicWorkRequest
. if you look at documentation of the the PeriodicWorkRequest.Builder
constructor that you are using, you will see that it says following about the second parameter
重复间隔必须大于或等于 PeriodicWorkRequest.MIN_PERIODIC_INTERVAL_MILLIS.
The repeat interval must be greater than or equal to PeriodicWorkRequest.MIN_PERIODIC_INTERVAL_MILLIS.
PeriodicWorkRequest.MIN_PERIODIC_INTERVAL_MILLIS
的值是900000,等于15分钟.
And the value of PeriodicWorkRequest.MIN_PERIODIC_INTERVAL_MILLIS
is 900000, meaning it is equal to 15 minutes.
这篇关于如何每5秒重试一次WorkManager的工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!