What could cause AsyncTask to fail?

I have a very sporadic error in my application that I am trying to solve. When you enter the application at some point, the main processing of the user interface threads ends and transfers control to the background thread to extract some data. When data is retrieved, control passes to the main user interface thread to process it for display. However, in some rare cases (99% of the time), AsyncTask does not seem to be invoked, leaving the application in a bad static state forever, waiting for AsyncTask to complete.

Here's a snapshot of the code in Activity :

 //method call from main UI thread private void fetchSomeData() { Log.d("myTag", "In fecthSomeData()"); new ReadFileAsyncTask<DataModel>().execute(this); } 

Here's the implementation of ReadFileAsyncTask :

 public class ReadFileAsyncTask<A> extends AsyncTask<I_ReadFileListener<A>, Void, A> { I_ReadFileListener<A> listener; @Override @SuppressWarnings("unchecked") protected A doInBackground(I_ReadFileListener<A>... params) { listener = params[0]; Log.d("mytag", "BACKGROUND: Loading " + listener.getFilename() + " from disk"); A fileContents = (A) FileUtils.readDataFromInternalStorage(listener.getContext(), listener.getFilename()); return fileContents; } @Override protected void onPostExecute(A result) { Log.d("myTag", "FOREGROUND: Executing onFileRetrieved listener"); listener.onFileRetrieved(result); } } 

Capturing logs on these rare errors:

 In fetchSomeData() ... (Other log messages from other interactions with the activity such as menu creation and navigation initialization) 

but, importantly, not the log statement from the second line of code in the doInBackground method. One thought that I had was that this log statement failed, but I do not see any power stop messages, errors in my logs or ACRA crash reports. The application is still active (I can move on to other actions and vice versa), so I don’t understand what could prevent this background thread from working correctly. Any ideas?

+4
source share
1 answer

Sadly, AsyncTask not suitable for executing critical code, because, depending on the base and maximum size of ThreadPool , your AsyncTask will never be able to execute.

In addition, the onPostExecute method could be called when the Activity it refers to (i.e. its creation context) is already destroyed. You have no way to synchronize it, and then, possibly using join () in AsyncThread from the user interface thread.

Despite the fact that I saw this in the Android Camera application, it is not recommended to block the UI thread waiting for events, since you will receive an ANR notification (the application does not work).

Take a look at this: Is AsyncTask conceptually wrong or am I just missing something?

Consider using IntentServices , HandlerThread or ThreadPoolExecutors if you need a possibly better way to synchronize your workflow with your UIThread.

From http://developer.android.com/training/run-background-service/create-service.html :

In addition, the IntentService is not affected by most UI life cycle events, so it continues to work in circumstances that would close AsyncTask

+1
source

All Articles