This may sound like a strange question but I'm wondering if AsyncTasks in Android Java have a habit of re-executing themselves multiple times just milliseconds apart?
The reason I'm asking is that I have a number of AsyncTasks in the app I'm developing. One of them is called DeleteSale and I execute it when the user has indicated that they want to delete a sale in the RecyclerView. My doInBackground() method executes a PHP script which deletes a single row of the MySQL table and passes back a JSON object to confirm that it has (or hasn't) deleted the row. I read that JSON object and if exactly one row was deleted, I'm done and execution proceeds to the onPostExecute() method. If the delete was not successful, I log a message saying that the delete failed, then proceed to onPostExecute().
During my testing, I tried to delete a single sale, selected from the dozen or so sales in my RecyclerView. By querying MySQL directly, I verified that the row WAS deleted successfully. However, when I looked at Logcat, I saw *FIVE* messages saying that it hadn't succeeded in deleting the row!!! (The timestamps on the messages were only milliseconds apart, which means there's no way I could have selected and deleted the rows manually by clicking on things in the app.)
It looks very much like the AsyncTask did the delete, succeeded, then ran again an additional five times, each of which inevitably failed because you obviously can't delete what isn't there any more. Yet I only invoked the AsyncTask ONCE.
Being new to the world of AsyncTasks, is there any chance that this behaviour is normal and "working as designed"? If so, why does it repeat the executions? Otherwise, how am I suppose to design my code to handle this?
Here is my DeleteSale private class so you can see for yourself that there is no loop within it:
Can anyone make sense of this for me?
The reason I'm asking is that I have a number of AsyncTasks in the app I'm developing. One of them is called DeleteSale and I execute it when the user has indicated that they want to delete a sale in the RecyclerView. My doInBackground() method executes a PHP script which deletes a single row of the MySQL table and passes back a JSON object to confirm that it has (or hasn't) deleted the row. I read that JSON object and if exactly one row was deleted, I'm done and execution proceeds to the onPostExecute() method. If the delete was not successful, I log a message saying that the delete failed, then proceed to onPostExecute().
During my testing, I tried to delete a single sale, selected from the dozen or so sales in my RecyclerView. By querying MySQL directly, I verified that the row WAS deleted successfully. However, when I looked at Logcat, I saw *FIVE* messages saying that it hadn't succeeded in deleting the row!!! (The timestamps on the messages were only milliseconds apart, which means there's no way I could have selected and deleted the rows manually by clicking on things in the app.)
It looks very much like the AsyncTask did the delete, succeeded, then ran again an additional five times, each of which inevitably failed because you obviously can't delete what isn't there any more. Yet I only invoked the AsyncTask ONCE.
Being new to the world of AsyncTasks, is there any chance that this behaviour is normal and "working as designed"? If so, why does it repeat the executions? Otherwise, how am I suppose to design my code to handle this?
Here is my DeleteSale private class so you can see for yourself that there is no loop within it:
Java:
private class DeleteSale extends AsyncTask<String, Void, String> {
protected String doInBackground(String... params) {
String clientCode = params[0];
String orderNumber = params[1];
OkHttpClient client = new OkHttpClient();
Request request = new Request.Builder()
.url(DATABASE_PATH + "deleteOneTwSale.php?clientCode=" + clientCode + "&orderNumber=" + orderNumber)
.build();
try {
Response response = client.newCall(request).execute();
JSONObject object = new JSONObject(response.body().string());
int rowCount = Integer.parseInt(object.getString("RowsDeleted"));
if (rowCount == 1) {
return WORKED;
} else {
Log.e(TAG + " DeleteSale - doInBackground()", getString(R.string.message010, orderNumber, clientCode, rowCount));
return FAILED;
}
} catch (IOException excp) {
excp.printStackTrace();
Log.e(TAG + " DeleteSale - doInBackground()", "IOException", excp);
return FAILED;
} catch (JSONException excp) {
excp.printStackTrace();
Log.e(TAG + "DeleteSale - doInBackground()", "JSONException", excp);
return FAILED;
}
}
protected void onPostExecute(String result) {
if (result == WORKED) {
ListSales.this.mSalesList.remove(position);
mAdapter.notifyItemRemoved(position);
}
}
Can anyone make sense of this for me?