Android AsyncTask and How To Make It Less Painful

If you are an android developer you probably know for doing tasks that take too much time from processor or tasks that keep the user waiting for couple of seconds if you run such task on current thread, android will  freeze the UI therefore you have to develop your application in multi-threading fashion and one of the solutions for making the app multi-threaded is by using AsyncTask abstract class.

Let us check the way of using AsyncTask in a very simplistic example :

(Assume you want to get a page from a url) :

 private class DownloadFilesTask extends AsyncTask {
     protected Long doInBackground(URL... urls) {
         int count = urls.length;
         long totalSize = 0;
         for (int i = 0; i < count; i++) {
             totalSize += Downloader.downloadFile(urls[i]);
             publishProgress((int) ((i / (float) count) * 100));
             // Escape early if cancel() is called
             if (isCancelled()) break;
         }
         return totalSize;
     }
 
     protected void onProgressUpdate(Integer... progress) {
         setProgressPercent(progress[0]);
     }
 
     protected void onPostExecute(Long result) {
         showDialog("Downloaded " + result + " bytes");
     }
 }

For executing AsyncTask:

new DownloadFilesTask().execute(url1, url2, url3);

Reference : http://developer.android.com/reference/android/os/AsyncTask.html

But what if you want to use AsyncTask in several different places?
Suppose you want to do several different tasks (e.g. counting a long list or process a text, etc…) If you want to implement AsyncTask everywhere you will end up with a stack of codes which are literally doing the same thing but with very minor changes.

So I thought how can I make it less painful?
I started to write one generic class for AsyncTsak to make it generic and I ended up with something as  following :

But its not the whole story for using this generic class I invent a technique (not quite sure if I can call it “design pattern”) to make it easier to use you need to create following interface as well:

Then inside your actual class you need to implement the ITask and then initialize  the overridden  methods then at the end instantiate the ParrelelTask class to run the actual AsyncTask class behind the scene.

here you can download the sample code to see it in action.

Conclusion

By the help of this technique I manage to reduce lines of codes from several classes inside my real application but yet this technique is not efficient and I think it still can be polished to become less confusing, and remember AsyncTask is not very efficient  in terms of doing several tasks at the same time. The whole idea of AsyncTask is keeping away the UI from freezing for having an actual muti-thread application you need to use JAVA threading APIs.

At the end let me know if you have better way of designing a technique or pattern for calling AsyncTask.

Leave a Reply

Your email address will not be published. Required fields are marked *


*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>