How to Observe the live data in work manager with chain of works?
Asked Answered
C

2

5

I have 3 tasks A,B, and C . I want to observe the live data for this chain and have a progress bar that shows the work in progress and once work its completed it should disable the progress bar.

    // One Time work for A class
         OneTimeWorkRequest Awork = new OneTimeWorkRequest
                          .Builder(A.class)
                          .setConstraints(Miscellaneous.networkConstraint())
                          .addTag("A")
                          .build();
            //same for B and C
            //work chain 
           WorkContinuation syncChain = WorkManager.getInstance()
                              .beginWith(Awork)
                              .then(Bwork)
                              .then(Cwork);

         syncChain.enqueue();
Charlenecharleroi answered 4/12, 2018 at 12:45 Comment(0)
H
5

You can use the mWorkManager.getWorkInfosByTagLiveData(TAG_OUTPUT); method to recover the status of your WorkRequest as shown in the WorkManager codelab.

This allows you to retrieve the status from the WorkRequest as a 'WorkInfo.State' enum with these possible states:

  • BLOCKED
  • CANCELLED
  • ENQUEUED
  • FAILED
  • RUNNING
  • SUCCEEDED

However, I'm not sure that this gives you enough granularity to manage a progress bar.

Retrieving the information as I wrote above gives you the flexibility to retrieve the WorkInfo for every WorkRequest.
As an alternative you can retrieve a list of WorkInfo for the WorkContinuation:

public abstract LiveData<List<WorkInfo>> getWorkInfosLiveData 
Huntress answered 4/12, 2018 at 14:53 Comment(4)
How to give a tag to "WorkContinuation" ? i have 3 task with 3 different tag names that are then added in the chain . So should i have a common tag name for all the three task and then add it to workmanager? @HuntressCharlenecharleroi
For WorkContinuations you can use directly their getWorkInfosLiveData method to retrieve a List of WorkInfo: developer.android.com/reference/androidx/work/… I'll update the reply adding this reference.Huntress
@Huntress I am just wondering How LiveData is associated with WorkManager?Himyaritic
WorkManager integrates support for LiveData: it is how you get an observable for your WorkInfo.Huntress
P
7

Just for illustrating the answer with a quick example

final int TASK_COUNT = 4;
mProgressBar = findViewById(R.id.progressbar);
mProgressBar.setMax(TASK_COUNT);

// One Time work for A class
OneTimeWorkRequest Awork = new OneTimeWorkRequest
              .Builder(A.class)
              .setConstraints(Miscellaneous.networkConstraint())
              .addTag("A")
              .build();
//same for B and C
//work chain 
WorkContinuation syncChain = WorkManager.getInstance()
                  .beginWith(Awork)
                  .then(Bwork)
                  .then(Cwork);

syncChain.enqueue();

syncChain.getWorkInfosLiveData().observe(this, new Observer<List<WorkInfo>>() {
    @Override
    public void onChanged(List<WorkInfo> workInfos) {

        int finishedCount = 0;

        for (WorkInfo workInfo : workInfos) {
            if (workInfo.getState().isFinished() && workInfo.getState() == WorkInfo.State.SUCCEEDED) {
                finishedCount++;
            }
        }
        mProgressBar.setProgress(finishedCount);

        if (finishedCount == workInfos.size()) {
             mProgressBar.setEnabled(false);
        }
    }
});
Playful answered 8/12, 2019 at 1:23 Comment(0)
H
5

You can use the mWorkManager.getWorkInfosByTagLiveData(TAG_OUTPUT); method to recover the status of your WorkRequest as shown in the WorkManager codelab.

This allows you to retrieve the status from the WorkRequest as a 'WorkInfo.State' enum with these possible states:

  • BLOCKED
  • CANCELLED
  • ENQUEUED
  • FAILED
  • RUNNING
  • SUCCEEDED

However, I'm not sure that this gives you enough granularity to manage a progress bar.

Retrieving the information as I wrote above gives you the flexibility to retrieve the WorkInfo for every WorkRequest.
As an alternative you can retrieve a list of WorkInfo for the WorkContinuation:

public abstract LiveData<List<WorkInfo>> getWorkInfosLiveData 
Huntress answered 4/12, 2018 at 14:53 Comment(4)
How to give a tag to "WorkContinuation" ? i have 3 task with 3 different tag names that are then added in the chain . So should i have a common tag name for all the three task and then add it to workmanager? @HuntressCharlenecharleroi
For WorkContinuations you can use directly their getWorkInfosLiveData method to retrieve a List of WorkInfo: developer.android.com/reference/androidx/work/… I'll update the reply adding this reference.Huntress
@Huntress I am just wondering How LiveData is associated with WorkManager?Himyaritic
WorkManager integrates support for LiveData: it is how you get an observable for your WorkInfo.Huntress

© 2022 - 2024 — McMap. All rights reserved.