Nicholas Muir Nicholas Muir - 1 month ago 17
Android Question

AsyncTask when is onPostExecute() not called?

I am trying to ensure that I account for all possibilities to avoid errors for the user.

I have a AsyncTask that implements an interface and returns a response to the calling class in onPostExecute of the AsyncTask as below:

public class GetVideoInfoFromDataBase extends AsyncTask {

Context context;
private AsyncInterface asyncInterface;

// Paginated list of results for alarm database scan
static PaginatedScanList<AlarmDynamoMappingAdapter> results;

// The DynamoDB object mapper for accessing DynamoDB.
private final DynamoDBMapper mapper;

public GetVideoInfoFromDataBase(Context context, AsyncInterface asyncInterface){
mapper = AWSMobileClient.defaultMobileClient().getDynamoDBMapper();
this.context = context;
this.asyncInterface = asyncInterface;

}

@Override
protected Object doInBackground(Object[] params) {
System.out.println("The backgrond stuff is working 1");
DynamoDBScanExpression scanExpression = new DynamoDBScanExpression();
results = mapper.scan(AlarmDynamoMappingAdapter.class, scanExpression);
return results;
}

@Override
public void onPostExecute(Object obj) {

asyncInterface.response((PaginatedScanList<AlarmDynamoMappingAdapter> )obj);
}
}


Is there any time that onPostExecute would not be called meaning the interface would not return a value?

Thanks in advance for your help.

Answer

onPostExecute() will run as long as doInBackground() returns and you did not cancel the task explicitly (by calling cancel() on your AsyncTask instance).

Technically, onPostExecute() will get called after (and if) the following line gets executed successfully:

return results;

As long as your process does not get killed in the meantime.

If you've cancelled your task, onCancelled() will be invoked instead of onPostExecute().

Comments