• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

Do Do AsyncTasks re-execute themselves automatically?

RhinoCan

Well-Known Member
Jul 5, 2015
184
64
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:
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?
 
The answer to your question is no, AsyncTasks don't repeat themselves automatically.

A simple way to verify how many times your code is executed, would be to place a breakpoint in your doInBackground() method. How many times does it get hit?

That's why I posted: I *was* running in debug so knew I had only executed doInBackground() once. Yet it apparently executed 5 or 6 times based on the messages I saw in the log.

Naturally, it is very reassuring to know that if you execute an AsyncTask once, it is only supposed to execute once. That's what I was hoping for as anything else would be disturbing and unsettling to say the least, although possibly justifiable under some circumstances.

Unfortunately, that leaves me quite baffled. If I only execute the AsyncTask, then why does the log show it executing 5 times (and really 1 additional time before that because successful completion of the delete does not generate a log message)?

This clearly bears further investigation....
 
Upvote 0

BEST TECH IN 2023

We've been tracking upcoming products and ranking the best tech since 2007. Thanks for trusting our opinion: we get rewarded through affiliate links that earn us a commission and we invite you to learn more about us.

Smartphones