Workermanager REPLACE 会影响已经 运行 个实例吗?

Workermanager REPLACE will affect already running instance?

我们在队列中收到很多请求。当我们收到任何请求时,我们正在实例化 workermanager 工作。 ExistingWorkPolicy.REPLACE 是如何工作的?

文档说

If there is existing pending (uncompleted) work with the same unique name, cancel and delete it.

它还会杀死中间现有的 运行 工人吗?我们确实不希望现有的worker中途停止,当worker入队但不是运行状态时被替换是可以的。我们可以在这里使用 REPLACE 选项吗?

https://developer.android.com/reference/androidx/work/ExistingWorkPolicy

WorkManager's guide 和您的问题中所述,当您使用 REPLACE 作为现有工作人员策略将新的 UniqueWorkRequest 入队时,这将停止当前 [=24] 的前一个工作人员=].

您的工作人员会发生什么情况实际上取决于您如何实施它(WorkerCoroutineWorker 或另一个 ListenableWorker 子类)以及您如何处理停工和取消]2.

这意味着您的 Worker 需要“合作”完成并清理您的 Worker:

In the case of unique work, you explicitly enqueued a new WorkRequest with an ExistingWorkPolicy of REPLACE. The old WorkRequest is immediately considered terminated.

Under these conditions, your worker will receive a call to ListenableWorker.onStopped(). You should perform cleanup and cooperatively finish your worker in case the OS decides to shut down your app.