Reputation: 3103
I have an AsyncTask to do SQLite database migration in the background (create or upgrade). Let's say somehow an IOException or SQLiteException is thrown inside doInBackground and it's pointless for the app to continue running because database state might be not in desired state. I'm kind of confused on what to do in this situation.
I'm thinking about letting the application crash as soon as possible and show dialog with error message, but I'm not really sure how to this inside doInBackground, because:
Anyone has an advice on how to gracefully handle this situation?
Upvotes: 11
Views: 12621
Reputation: 4111
My approach to handle it.
/**
* Created by Daniel on 02/04/2016.
*/
public abstract class AsyncTaskEnhanced<Params, Progress, Result> extends AsyncTask<Params, Progress, Result> {
// Avoids cyclic calls.
private boolean onPostExecuteCalled = false;
private Exception exception;
@Override
protected final Result doInBackground(Params... params) {
try {
return this.doInBackgroundWithFaultTolerance(params);
} catch (Exception exception) {
this.exception = nre;
}
return null;
}
@Override
protected final void onPostExecute(Result result) {
if (this.onPostExecuteCalled) return;
this.onPostExecuteCalled = true;
super.onPostExecute(result);
this.onPostExecuteWithFaultTolerance(result, this.exception);
}
protected abstract Result doInBackgroundWithFaultTolerance(Params... params) throws Exception;
protected abstract void onPostExecuteWithFaultTolerance(Result result, Exception ex);
}
Upvotes: 0
Reputation: 4467
You can't show dialog in non-ui thread. You can pass activity reference to async task. To handle this situation you can try catch the exception in doInBackground and re-throw it in onPostExecute
e.g.
private class MyAsyncTaskTask extends AsyncTask<...> {
private Activity ownerActivity;
private Exception exceptionToBeThrown;
public MyAsyncTaskTask(Activity activity) {
// keep activity reference
this.ownerActivity = activity;
}
protected Long doInBackground(...) {
try {
...
} catch (Exception e) {
// save exception and re-thrown it then.
exceptionToBeThrown = e;
}
}
protected void onPostExecute(...) {
// Check if exception exists.
if (exceptionToBeThrown != null) {
ownerActivity.handleXXX();
throw exceptionToBeThrown;
}
}
}
If you async task is in Acvitiy class, then you can directly access it, e.g.,
public class MyActivity extends Activity {
...
AsyncTask<...> task = new AsyncTask<...>() {
public void onPostExecute(...) {
// Access activity directly
MyActivity.this.xxx()
}
}
}
Upvotes: 19
Reputation: 5271
return a unique string everytime such an exception occurs in doInBackground to onPostExecute. and in onPostExecute display a AlertDialog showing appropriate message and ask to try again or something.
Upvotes: 0