gpt4 book ai didi

android - 我们可以使用 WorkManager 在其他 UniqueWork 的队列中添加另一个 UniqueWork

转载 作者:行者123 更新时间:2023-11-29 23:19:17 25 4
gpt4 key购买 nike

假设我已经用 WorkManager 开始了这个 UniqueWork :-

var workContinuation: WorkContinuation = WorkManager.getInstance()
.beginUniqueWork(AppConstants.FILE_CREATOR_TAG,
ExistingWorkPolicy.APPEND,
firstOneTimeWorkRequest)

此时,我们的应用程序用户可能想要为其他任务启动另一个 UniqueWork 工作,但该任务应添加到上述工作任务的队列中。

var workContinuation: WorkContinuation = WorkManager.getInstance()
.beginUniqueWork(AppConstants.FILE_DISTRIBUTE_TAG,
ExistingWorkPolicy.APPEND,
fileDistrubuteTimeWorkRequest)

现在我如何告诉 WorkManager 这个 UniqueWork AppConstants.FILE_DISTRIBUTE_TAG 应该在这个 AppConstants.FILE_CREATOR_TAG 的队列中。

我不想使用 Observable 解决方案,在该解决方案中我可以跟踪此 AppConstants.FILE_CREATOR_TAG 工作人员完成并启动 AppConstants.FILE_DISTRIBUTE_TAG

最佳答案

您应该为两个工作人员指定相同的 uniqueWorkName。引用 beginUniqueWork documentation :

The uniqueWorkName uniquely identifies this set of work.

If this method determines that new work should be enqueued and run, all records of previous work with uniqueWorkName will be pruned. If this method determines that new work should NOT be run, then the entire chain will be considered a no-op.

If any work in the chain fails or is cancelled, all of its dependent work inherits that state and will never run. This is particularly important if you are using APPEND as your ExistingWorkPolicy.

因此,你的代码应该这样写:

var workContinuation: WorkContinuation = WorkManager.getInstance()
.beginUniqueWork(AppConstants.SOME_UNIQUE_NAME,
ExistingWorkPolicy.APPEND,
firstOneTimeWorkRequest)

// ...

var workContinuation: WorkContinuation = WorkManager.getInstance()
.beginUniqueWork(AppConstants.SOME_UNIQUE_NAME,
ExistingWorkPolicy.APPEND,
fileDistrubuteTimeWorkRequest)

关于android - 我们可以使用 WorkManager 在其他 UniqueWork 的队列中添加另一个 UniqueWork,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54705252/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com