Wait publishProgress finish before continue excute

2020-07-24 08:04发布

My AsyncTask look like:

private class MyTask extends AsyncTask<String, Void, List<Data>> {
  private volatile UserData userData;
  protected List<Data> doInBackground(String... params) {
      // do some job 1
      publishProgress();
      // wait until the progress finish to update variable.
      if(userData!= null){
        //do some job 2
      }

      //do some job 3

  }

    protected void onProgressUpdate(Void... values) {
        // try to ask user and collect information
        userData= getData();
    }

}

The problem is when I call publishProgress(), it still execute job 3 in doInBackground. How can I wait onProgressUpdate complete before continue?

Update:
In the getData() method, I tried to get the current location of user, which can't work in doInBackground and some other information from the view. I have to do this cause the job 1 take a very long time, and the current location of user can be changed. I need the most exactly location of user.

3条回答
神经病院院长
2楼-- · 2020-07-24 08:39

use a flag _publishFinished = false; then call publish progress then do busy waiting in doInBackgrnd

`if(!_publishFinish) 
{Thread.Sleep(200);}`

at end of onProgressUpdate call

_publisFinish = true;
查看更多
男人必须洒脱
3楼-- · 2020-07-24 08:43
private class MyTask extends AsyncTask<String, Void, List<Data>>{


        protected void onPreExecute() {
            // Showing progress dialog before sending http request
            pDialog = new ProgressDialog(YourActivity.this);
            pDialog.setMessage("Please wait..");
            pDialog.setIndeterminate(true);
            pDialog.setCancelable(false);
            pDialog.show();
        }
  private volatile UserData userData;
  protected List<Data> doInBackground(String... params) {
      // do some job 1
      publishProgress();
      // wait until the progress finish to update variable.
      if(userData!= null){
        //do some job 2
      }

      //do some job 3

  }

    protected void onProgressUpdate(Void... values) {
        // try to ask user and collect information
        userData= getData();
    }
    protected void onPostExecute(Void unused) {
            // closing progress dialog
            pDialog.dismiss();
        }

}

pDialog is your progress dialog. initialize from anywhere of your activity like this:

  ProgressDialog pDialog;

This will might help you.

查看更多
狗以群分
4楼-- · 2020-07-24 08:47

I think, the way you are using doInBackground and publishProgress() is something wrong, but still I need to see the code of method getData() .

doInBackground method should be used to do some task, which should be executed in other thread than event thread, and in publishProgress method, you should execute tasks which works with event thread. So, if getData() has nothing to do with event thread, please write the code in doInBackground itself.

However, if your requirements are just use both these methods, as present, then you can add a synchronize block, as like below:

private class MyTask extends AsyncTask<String, Void, List<Data>> {
  private volatile UserData userData;
  protected List<Data> doInBackground(String... params) {

  synchronize(userData)
  {
      // do some job 1
      publishProgress();
      // wait until the progress finish to update variable.
      if(userData!= null){
        //do some job 2
      }

      //do some job 3
    }
  }

    protected void onProgressUpdate(Void... values) {
        // try to ask user and collect information
        synchronize(userData)
        {
            userData= getData();
        }
    }

}
查看更多
登录 后发表回答