Skip to content Skip to sidebar Skip to footer

Down Sides Of Async Task If Any

I know you are using it quite well with your Non-UI codes in AsnycTask but I am just wondering if there is any kind of problem while using AsyntTask? I am not having any code which

Solution 1:

  1. Memory Leak :

Even though activity is destroyed, AsyncTask holds the Activity's reference since it has to update UI with the callback methods.

  1. cancelling AsyncTask :

cancelling AsyncTask using cancel() API will not make sure that task will stop immediately.

  1. Data lose :

When screen orientation is done. Activity is destroyed and recreated, hence AsysncTask will hold invalid reference of activity and will trouble in updating UI.

Solution 2:

  • Concurrent AsyncTasks: Open Asynctask.java go to line number 199, it shows you can create only 128 concurrent tasks private static final BlockingQueue<Runnable> sPoolWorkQueue = new LinkedBlockingQueue<Runnable>(128);
  • Rotation: When Activity is restarted, your AsyncTask’s reference to the Activity is no longer valid, so onPostExecute() will have no effect.
  • Cancelling AsyncTasks: If you AsyncTask.cancel() it does not cancel your AsyncTask. It’s up to you to check whether the AsyncTask has been canceled or not.
  • Lifecycle:AsyncTask is not linked with Activity or Fragment, so you have to manage the cancellation of AsyncTask.

There are some workarounds to solve above issues for more details have a look at The Hidden Pitfalls of AsyncTask

Solution 3:

I just want to share the information that if you are using Asynctask, it will keep on doing its work even of the activity does not exist. So in case you have asynctask which starts in onCreate() of the activity, and you rotate the device. At each rotation, a new activity is created with a new instance of Asysntask. So many requests will be send over the network for same task.In this way, a lot of memory will be consumed which effects the app performance resulting in crashing it. So to deal with it Loaders(Asynctask Loaders) are used. For more info check the video: Loaders

Post a Comment for "Down Sides Of Async Task If Any"