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?
source share