Down Sides Of Async Task If Any
Solution 1:
- Memory Leak :
Even though activity is destroyed, AsyncTask holds the Activity's reference since it has to update UI with the callback methods.
- cancelling AsyncTask :
cancelling AsyncTask using cancel() API will not make sure that task will stop immediately.
- 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 tasksprivate static final BlockingQueue<Runnable> sPoolWorkQueue = new LinkedBlockingQueue<Runnable>(128);
- Rotation: When
Activity
is restarted, yourAsyncTask
’s reference to theActivity
is no longer valid, soonPostExecute()
will have no effect. - Cancelling AsyncTasks: If you
AsyncTask.cancel()
it does not cancel yourAsyncTask
. It’s up to you to check whether the AsyncTask has been canceled or not. - Lifecycle:
AsyncTask
is not linked withActivity
orFragment
, so you have to manage the cancellation ofAsyncTask
.
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"