Android Worker ExistingWorkPolicy.APPEND_OR_REPLACE is not acting as expected
Asked Answered
H

1

0

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.

Hypocycloid answered 2/3, 2022 at 8:31 Comment(0)
T
3

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.

Toddle answered 29/3, 2022 at 6:19 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.