Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Running same Asynctask multiple times sequentially

I am trying to execute same Asynctask sequentially. For example, Let there be an Asynctask A. Now assume A checks if some server is busy or not. If it's busy, it checks again.... And so on. Let's say it checks this for 5 times then let users know that the service is unavailable.
So same Asynctask can obviously be used.
Trying to achieve it with loops may yield unexpected result as according to this link,
Asynctask is a fire-and-forget instance and AsyncTask instances can only be used one time..
So looping 5 times means Asynctask will be called 5 times. But if the server is free on the 2nd attempt, I don't need additional checking. Also android application may hang(mine did). Correct me if I'm wrong on this topic.

Also the answer was, "calling task like new MyAsyncTask().execute("");"
So if i do like this -- for example,

new someAsyncTask() {
            @Override
            protected void onPostExecute(String msg) {
                new someAsyncTask() {
                    @Override
                    protected void onPostExecute(String msg) {
                      ....and so on 5 times.
                   }
               }.execute(this);
            }
        }.execute(this);

It should be called 5 times sequentially and no problems should occur.
Is it the only way to do this or any other solution is present?

like image 946
Shubhashis Avatar asked May 02 '15 16:05

Shubhashis


People also ask

How many times an instance of AsyncTask can be executed?

AsyncTask instances can only be used one time.

Can we run multiple async task at a time?

NOTE : As for limitations, we should mention that AsyncTask always has to executed from the main thread and we can't call execute() twice in the same object; therefore, they cannot loop. if we execute more than one AsyncTask at a time, they will run sequentially by the default version from Android 3.0 onwards.

What are the disadvantages of AsyncTask in Android?

"It is not a good idea to use ' AsyncTask ' as it is not attached to an activity life cycle. The virtual machine will hold on to the activity object as long as the Asynctask is running, even after Android has called onDestroy( ) method for the activity and expect it to be discarded.

Why did AsyncTask get deprecated?

Official Reason for Deprecation of AsyncTask AsyncTask was intended to enable proper and easy use of the UI thread. However, the most common use case was for integrating into UI, and that would cause Context leaks, missed callbacks, or crashes on configuration changes.


1 Answers

You could use a field variable in the outer class:

private int count;
private void attemptConnect(){
    count = 0;

    new MyTask().execute("");
}

class MyTask extends AsyncTask<String, String, String> {
    @Override
    protected String doInBackground(String... params) {
        return null;
    }

    @Override
    protected void onPostExecute(String s) {
        count++;
        if(count < 5){
            new MyTask().execute("");
        }
    }
}

Or pass in a count to the AsyncTask constructor:

private void attemptConnect(){
    new MyTask(0).execute("");
}

class MyTask extends AsyncTask<String, String, String> {
    private int count;

    public MyTask(int count){
        this.count = count;
    }

    @Override
    protected String doInBackground(String... params) {
        return null;
    }

    @Override
    protected void onPostExecute(String s) {
        if(count < 4){
            new MyTask(++count).execute("");
        }
    }
}
like image 188
tachyonflux Avatar answered Nov 09 '22 13:11

tachyonflux