Skip to content Skip to sidebar Skip to footer

Asynctask's Doinbackground Starts Its Execution Too Late After Asynctask::execute Is Called

I wrote an AsyncTask and most of the time there is no delay between its constructor been called and its doInBackground been called (0 ms delay). But whenever contacts syncing is ha

Solution 1:

I also face this issue for long period, but now it is solved. Use the code below

new AsyncTaskName().executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);

instead the code

new AsyncTaskName().execute();

it would solve the problem of running doInbackground late.

Solution 2:

We generally don't bother about task scheduling by the jvm. And infact we need not to bother also.

If something needs to be done as quick as possible in your application do it in the constructor itself or use onPre of Asynctask (Remember it execute on UI thread).

But i agree there is something fishy in the doInBackgroud calling in Android AsyncTask i itself had witnessed the doInbackground didn't get called after onPre. You can google this also. many people had faced it. I moved to use Traditional Thread.

I wrote my own Asynctask using Traditional thread at core and to mimic onPre and onPost i used Handler. You can go for that option also.

Solution 3:

It's important to distinguish between creating and executing the task. (An ASyncTask has a separate execute() method, as well as a constructor, as you have noticed.)

Creating a thread is potentially quite expensive, so you might find that creating the task in advance, and then only executing it at the correct time, gives better results.

If the background operation is likely to be repeated often, you may also find that an IntentService, which handles requests one-at-a-time in a background thread, is more suitable.

Post a Comment for "Asynctask's Doinbackground Starts Its Execution Too Late After Asynctask::execute Is Called"