I a*_*ent 4 android kotlin android-jetpack android-workmanager
By referring to: Android WorkManager api for running daily task in Background
It uses the WorkManager.enqueueUniquePeriodicWork to ensure that PeriodicWorkRequest is not created multiple times.
example code:
val work = PeriodicWorkRequestBuilder<SyncWork>(15,TimeUnit.MINUTES).build()
WorkManager.getInstance().enqueueUniquePeriodicWork("TaskTag",
ExistingPeriodicWorkPolicy.KEEP, work);
Run Code Online (Sandbox Code Playgroud)
However, I found it there is 2 option of ExistingPeriodicWorkPolicy which is ExistingPeriodicWorkPolicy.KEEP and ExistingPeriodicWorkPolicy.REPLACE can be use.
I try to implement it and run the code, but it does really show any differences, and it seem both of them behave the same way.
My uncertainty:
与 的ExistingPeriodicWorkPolicy.KEEP表现有何不同ExistingPeriodicWorkPolicy.REPLACE?
在您的示例中,您将要排队一个新的工作请求(又名工人)。如果您之前在运行时执行过此操作,则此工作器已存在状态为 ENQUEUED。
KEEP:如果之前的工作人员存在,则您的新尝试将被简单地忽略,否则您的新工作人员将入队。
REPLACE:如果前一个worker存在,它被取消,导致它的状态为CANCELLED。否则,您的新工作人员已入队。
因此,如果您确定您的新工人与前一个工人相同(假设约束没有改变),那么 KEEP 应该是安全的,否则 REPLACE 可能是更好的选择。
| 归档时间: |
|
| 查看次数: |
1966 次 |
| 最近记录: |