workstatus observer always in enqueued state

后端 未结 3 807
无人共我
无人共我 2021-01-20 05:54

I am trying to observe my workers but they are always in queued state or sometime it\'s RUNNING but never SUCCEED or

相关标签:
3条回答
  • 2021-01-20 06:16

    For your periodic work request you should see

    ENQUEUED - RUNNING - ENQUEUED
    

    where the latter ENQUEUED is the state of the next work request.

    You might get very briefly a SUCCEEDED between RUNNING and ENQUEUED, but I have never seen that.

    For a onetime work request you see

    ENQUEUED - RUNNING - SUCCEEDED
    

    or whatever you return in doWork().

    (Android 8.1 API 27, 1.0.0-alpha04)

    0 讨论(0)
  • 2021-01-20 06:27

    The above answer is correct. For PeriodicWork you should see:

    ENQUEUED -> RUNNING -> ENQUEUED

    However, there is a bug in alpha04 which causes PeriodicWork to not run on API >= 23. This will be fixed in alpha05. For more info take a look at https://issuetracker.google.com/issues/111195153.

    IMPORTANT: As of a couple of days ago: alpha05 has shipped. This bug is fixed.

    0 讨论(0)
  • 2021-01-20 06:29

    This is for anyone who is having trouble getting their output data from periodic work. It's more like a hack. In your Worker, just define a static mutable Live Data. At the place where you observe your work's state, observe this live data when your state turns to "RUNNING".

    Here's a template :

    1. The actual Worker:
    public class SomeWorker extends Worker{
    
       //This live data can be of any type. I'm setting Boolean
       Public static MutableLiveData<Boolean> outputObservable = new MutableLiveData();
       private boolean output_boolean;
       try{
           //Do you work here post your result to the live data
           output_boolean = SomeTaskThatReturnsABoolean();
           outputObservable.postValue(output_boolean);
           return Result.Success();
       }catch(Exception e){
           e.printStackTrace();
           outputObservable.postValue(!output_boolean);
           return Result.Failure();
        }
    }
    
    
    1. Your activity that observes this worker's info:
    //In YourActivity class inside OnCreate
    mWorkManager.getWorkInfoForUniqueWorkLiveData(YOUR_TAG).observe (this,
       new Observer<List<WorkInfo>>(){
           @Override
           public void onChanged(@Nullable List<WorkInfo> workInfos) {
              if(workInfos!=null && (!(workInfos.isEmpty()))) {
                 WorkInfo info = workInfos.get(0);
                 switch(info.getState()){
                   case ENQUEUED:
                       break;
                   case RUNNING:
                       SomeWorker.outputObservable.observe(YourActivity.this,
                          new Observer<Boolean>(){
                          @Override
                           public void onChanged(@Nullable Boolean aBoolean) {
                               //EDIT: Remove the observer of the worker otherwise 
                               //before execution of your below code, the observation might switch
                               mWorkManager.getWorkInfoForUniqueWorkLiveData(YOUR_TAG).removeObservers(YourActivity.this);
                               if(aBoolean)
                                 //Do whatever you have to if it's true
                               else
                                 //Do whatever you have to if it's false
                               }
                          }
                       );
                 }
              }
           }
       }
    );
    

    In this way you can observe your results when the state of the work is under running, before it gets switched back to enqueued.

    0 讨论(0)
提交回复
热议问题