Search code examples
androidandroid-workmanager

How to detect within an Android worker that within a sequence of Workers a previous worker has failed


Im investigating Android Workers.

api "androidx.work:work-runtime:2.6.0-alpha01"

Im creating unique work that entails multiple workers running in sequence

Worker_A -> Worker_B -> Worker_C

In the event either Worker_A or Worker_B returns Result.failure() is there any approach I can take to detect this within Worker_C


Solution

  • As explained in WorkManager's Chaining Work documentation:

    Chains of OneTimeWorkRequest execute sequentially as long as their work completes successfully (that is, they return a Result.success()). Work requests may fail or be cancelled while running, which has downstream effects on dependent work requests.

    To answer your question:

    In the event either Worker_A or Worker_B returns Result.failure() is there any approach I can take to detect this within Worker_C?

    No, Worker_C cannot detect a failure of Worker_A or Worker_B because it is not going to be run at all.

    Chaining and Worker statuses covers this in detail.