Search code examples
androidchainingandroid-workmanager

Chain one to many `OneTimeWorkRequest` objects dynamically


There are many ways to chain WorkRequest objects. Parallel, sequently and even combining multiple works.

Even though that this API seems to be very flexible, I couldn't find a way achieving the following task:

First Worker is responsible to gather information, at the end of its execution, it outputs a list of file names that should be manipulated. Afterwards, I would like to make manipulations on each of these files. So I would imagine having another Worker that is responsible to manipulate a single file. But I couldn't find a way to pipeline the output of each filename from the first Worker to multiple manipulator Worker objects. (There should be one manipulator WorkRequest for each of the files and these WorkRequest objects should run in parallel).

How can one WorkRequest create a dynamic number of WorkRequestobjects based on its output?


Solution

  • I don't know of any way to enqueue a dynamic number of WorkRequest. The idea is that you build a chain of WorkRequests that is fixed and can be processed by WorkManager as a whole (if every worker succeeds).

    For this particular case, I'll probably try create the required number of WorkRequests (one per file) when the initial WorkRequest succeeds.