Retry AsyncTask

For example, I have the following AsyncTask:

  private class MyAsyncTask extends AsyncTask<Void, Void, Boolean> {


     @Override
     protected Void doInBackground(Void... params) {
         try {
             //some code that may throws exception
             return true;
         } catch (IOException ex) {
             return false;
         }
     }


     @Override
     protected void onPostExecute(Boolean param){
        if (!param) {
           AlertDialog.Builder builder = new AlertDialog.Builder(MyActivity.this);
           builder.setMessage("Error");
           builder.setPositiveButton("Retry", new DialogInterface.OnClickListener() {
              public void onClick(DialogInterface dialog, int which) {
                  //I want to retry MyAsyncTask here
              } 
           });
           builder.setNegativeButton("Exit", new DialogInterface.OnClickListener() {
              public void onClick(DialogInterface dialog, int which) {
                 finish();
              } 
           });
           AlertDialog alert = builder.create();
           alert.show();
        }
     }
  }

What is the best way to do this? I am afraid of recursion of this code.

+3
source share
4 answers

You cannot strictly "repeat" an instance AsyncTask. You must create a new instance and execute it in the same way as the first. You should not face the problem of recursion.

+7
source

I repeat the code inside doInBackground()for x number of times:

 @Override
 protected Void doInBackground(Void... params)
 {
    final int MAX_RETRY=3;
    int iLoop;
    boolean bSuccess=true;
    for (iLoop=0; iLoop<MAX_RETRY; iLoop++)
    {
        try
        {
            //do some code that throw an exception

            //success! exit loop
            iLoop=0;
            break;
        }
        catch (IOException ex)
        {
            bSuccess=false;
        }
    }
    if (iLoop==(MAX_RETRY-1))
    {
        bSuccess=false;
    }
    return bSuccess;
 }

It is also one of those cases where two Boolean values ​​are inadequate to measure success. You can replace bSuccess with an enumeration for the third dimension of success: try again later.

+2
source

, BetterASyncTask.

It provides a handy abstract HandleError method that allows me to catch an exception in the user interface thread and decide whether to try again and how to do it.

+1
source

Please see retryableasynctask and see if this helps.

Using:

// Params, Progress and Result could be anything, same as a regular AsyncTask
new RetryableAsyncTask<Params, Progress, Result>(myActivity) {
  @Override
  protected void onPreExecute() {
    // write some code here
  }

  @Override
  protected Result doInBackground(Params... params) {
    // execute some expensive task here with your params
    // eg: MyExpensiveTask with method called 'get'

    return MyExpensiveTask.get(params);
  }

  @Override
  protected void onPostExecute(Result result) {
    // write some code here with your result
  }
}.execute(myParams);

Overriding onError Behavior

By default, the onError method shows the Cancel and Redo button options. However, you may want to do something else when something goes wrong. To do this, override onError using native error handling.

// Params, Progress and Result could be anything, same as a regular AsyncTask  
new RetryableAsyncTask<Params, Progress, Result>(myActivity) {
  // ...

  @Override
  protected void onError(Throwable error, final Params... params) {
    // write your own error handling
  }
}.execute(myParams);
0
source

All Articles