Android Worker ExistingWorkPolicy.APPEND_OR_REPLACE 未按预期运行

发布于 2025-01-11 05:02:55 字数 1295 浏览 4 评论 0原文

我当前的 Android 应用程序使用 androidx.work.CoroutineWorker 来执行长时间运行的下载任务。

api 'androidx.work:work-runtime:2.7.1'
api 'androidx.work:work-runtime-ktx:2.7.1'

下载工作器是工作器链的一部分,每个工作器都完成下载先决条件的一部分。

我已使用 ExistingWorkPolicy.APPEND_OR_REPLACE 使我的用户能够对下载请求进行排队。这种方法效果很好,而且每次下载都不会失败。

ExistingWorkPolicy.APPEND_OR_REPLACE 描述如下:-

If there is existing pending (uncompleted) work with the same unique name, append 
the newly-specified work as the child of all the leaves of that work sequence. 
Otherwise, insert the newly-specified work as the start of a new sequence. 
Note: If there are failed or cancelled prerequisites, these prerequisites are 
dropped and the newly-specified work is the start of a new sequence.

我对此的解释是,如果我对工作链的多个实例进行排队,如果第一个实例失败,第二个实例仍将正常启动。

但这不是我看到的行为。使用 Android Studio App Inspection -> 后台任务检查器 我看到我的第二个工作链在第一个链执行时全部被阻止,然后,当第一个链有一个工作程序失败时,整个第二个链也被标记为失败。

我怎样才能实现所需的行为?

注意:我已经读过这个问题,它与我的问题部分相同 WorkManager 现有工作策略 APPEND_OR_REPLACE 的行为不预计 但是我想知道如何实现所需的“预期”行为以及了解所看到的实际行为。

my current android application employs androidx.work.CoroutineWorker(s) to execute long running download tasks.

api 'androidx.work:work-runtime:2.7.1'
api 'androidx.work:work-runtime-ktx:2.7.1'

the download worker is part of a chain of workers that each complete part of the download prerequisites.

I have used ExistingWorkPolicy.APPEND_OR_REPLACE to enable my users to queue download requests. This approach works fine while each download does not fail.

ExistingWorkPolicy.APPEND_OR_REPLACE is described as follows:-

If there is existing pending (uncompleted) work with the same unique name, append 
the newly-specified work as the child of all the leaves of that work sequence. 
Otherwise, insert the newly-specified work as the start of a new sequence. 
Note: If there are failed or cancelled prerequisites, these prerequisites are 
dropped and the newly-specified work is the start of a new sequence.

I interpreted this to mean that if I queue multiple instances of my work chain, if the first was to fail the second would still start ok.

This is not the behaviour I am seeing though. Using Android Studio App Inspection -> Background Task Inspector I see my second chain of workers are all blocked while the first chain is executing, then, when the first chain has a worker that fails the complete second chain is also marked as failed.

how can I achieve the required behaviour?

NOTE: I have read this question which is partly the same as mine
WorkManager existing work policy APPEND_OR_REPLACE doesn't behave as expected
however I want to know how to achieve the desired "expected" behaviour as well as understand the actual behaviour seen.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

£冰雨忧蓝° 2025-01-18 05:02:55

根据 https://issuetracker.google.com/issues/134613984#comment7, APPEND_OR_REPLACE 仅在排队发生时应用,换句话说,当第二个请求排队时,如果第一个工作已经失败,那么第二个请求仍将执行(与文档匹配);但如果第一个工作尚未完成,那么第二个请求将作为第一个请求的依赖项而附加,导致一失败全失败(与 APPEND 相同)。

一个快速的肮脏解决方法是始终返回工作的成功;如果需要失败结果和/或取消工作,应用程序必须自行维护。

Accorindg to https://issuetracker.google.com/issues/134613984#comment7, APPEND_OR_REPLACE is only applied when the enqueuing happens, in other words, when the 2nd request was enqueued, if the 1st work already failed, then the 2nd request will still execute (matching the doc); but if the 1st work hasn't finished yet, then the 2nd request will be appended as dependent of the 1st request, resulting in one-fails-all-fail (the same as APPEND).

A quick dirty workaround is to always return SUCCESS for the work; if the failure result and/or cancelling work is needed, the app has to maintain it by itself.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文