【发布时间】:2021-05-03 17:02:43
【问题描述】:
调用 ExistingPeriodicWorkPolicy.REPLACE 只是启动另一个 workManager 它不会替换另一个,多个 workManager 在触发时正在运行而不是替换
val constraints = Constraints.Builder()
.setRequiredNetworkType(NetworkType.CONNECTED)
.build()
val workRequest = PeriodicWorkRequestBuilder<SongsWorker>(1, TimeUnit.DAYS)
.addTag(TAG)
.setConstraints(constraints)
.build()
WorkManager.getInstance(mContext).enqueueUniquePeriodicWork(
TAG, ExistingPeriodicWorkPolicy.REPLACE, workRequest)
日志表明工作经理至少要求取消,但执行仍在继续
I/WM-WorkerWrapper: Work [ id=6ed5487e-5d7b-4db5-a4c5-c22c562567e5, tags={
workerManagers.songs.SongsWorkerManager$SongsWorker } ] was cancelled
java.util.concurrent.CancellationException: Task was cancelled.
at androidx.work.impl.utils.futures.AbstractFuture.cancellationExceptionWithCause(AbstractFuture.java:1184)
at androidx.work.impl.utils.futures.AbstractFuture.getDoneValue(AbstractFuture.java:514)
at androidx.work.impl.utils.futures.AbstractFuture.get(AbstractFuture.java:475)
at androidx.work.impl.WorkerWrapper$2.run(WorkerWrapper.java:300)
at androidx.work.impl.utils.SerialExecutor$Task.run(SerialExecutor.java:91)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1167)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:641)
at java.lang.Thread.run(Thread.java:923)
我已经为工作管理器附加了一个监听器,日志是
2021-04-30 20:40:54.318 17868-17868 Fragment: ID = 9f8b3165-fc75-4f9a-ba39-41c2dc6a17b3
2021-04-30 20:40:54.408 17868-17868 Fragment: ID = 9f8b3165-fc75-4f9a-ba39-41c2dc6a17b3
2021-04-30 20:40:54.427 17868-17868 Fragment: ID = 9f8b3165-fc75-4f9a-ba39-41c2dc6a17b3
听者是:
val instance = WorkManager.getInstance(requireContext())
instance.getWorkInfosForUniqueWorkLiveData(SongsWorkerManager.TAG)
.observe(viewLifecycleOwner) { workInfo ->
workInfo.forEach {
Log.d(TAG, "ID = ${it.id}")
}
}
在触发工作管理器 3 次后,有 3 个相同的工作管理器在运行而不是被替换。
【问题讨论】:
标签: android android-workmanager