3

I am trying to implement a search bar that automatically searches as you type.

My idea is to have an AsyncTask which fetches the search data from the server, but I can't figure how exactly AsyncTask will behave with my use of it.

Let's say I have SearchAsyncTask.

Every time the text field is edited I will call

new SearchAsyncTask().execute(params);

So here's my question: what will the behavior of this be? Will I be starting many different threads which will all return and call onPostExecute()? Or will the first task called be stopped mid-task if another instance is called while it's still working? Or something totally different?

What if I write it this way?

SearchAsyncTask a = new SearchAsyncTask().execute(params);
...
a.execute(params2);
a.execute(params3);
...
TN888
  • 7,659
  • 9
  • 48
  • 84
Michael Hoyle
  • 249
  • 2
  • 9

2 Answers2

5

I have implemented my app's search function in the same way. I use a TextWatcher to build search results as and when the user types. I keep a reference of my AsyncTask to achieve this. My AsyncTask declaration:

SearchTask mySearchTask = null;  // declared at the base level of my activity

Then, in the TextWatcher, on each character input, I do the following:

// s.toString() is the user input
if (s != null && !s.toString().equals("")) {

    // User has input some characters

    // check if the AsyncTask has not been initialised yet
    if (mySearchTask == null) {

        mySearchTask = new SearchTask(s.toString());

    } else {

        // AsyncTask is either running or has already finished, try cancel in any case
        mySearchTask.cancel(true);

        // prepare a new AsyncTask with the updated input            
        mySearchTask = new SearchTask(s.toString());

    }

    // execute the AsyncTask                
    mySearchTask.execute();

} else {

    // User has deleted the search string // Search box has the empty string "" now

    if (mySearchTask != null) {

        // cancel AsyncTask 
        mySearchTask.cancel(true);

    }

    // Clean up        

    // Clear the results list
    sResultsList.clear();

    // update the UI        
    sResultAdapter.notifyDataSetChanged();

}
Vikram
  • 51,313
  • 11
  • 93
  • 122
0

Regarding your second question, I believe you could have found your answer on this site, here for instance. Basically, you can only execute a single instance of an AsyncTask once. Trying to execute twice the same instance will raise an error.

edit : If you declare each time a new SearchAsyncTask(), you will receive numerous responses and call to onPostExecute(), but not necessarily in the right order. What would be best is to store the AsyncTask in a variable, and use the .cancel() method before starting a new one.

Community
  • 1
  • 1
Nerkatel
  • 1,805
  • 16
  • 25
  • This doesn't really answer the question. In the first part of the question he is not executing the same instance. But it does answer the second question – codeMagic Jul 25 '13 at 16:47