开发者

"realtime" search using AsyncTask?

开发者 https://www.devze.com 2023-01-27 12:15 出处:网络
I am writing an application that searches a database in \"realtime\". i.e. as the user presses letters it updates a search results list.

I am writing an application that searches a database in "realtime". i.e. as the user presses letters it updates a search results list.

Since the search can take a while, I need to do the search in background and allow new key presses to re-start a search. So that is a user presses 'a' (and the code starts searching for "a"), then presses 'b' - the code will NOT wait for "a" search to end, then start searching for "ab", but rather STOP the "a" search, and start a new "ab" search.

  • To do that I decided to do the search in an AsyncTask. Is this a wise decision ?
开发者_如何学Python

Now - whenever a keypress is detected, I test to see if I have an AsyncTask running. If I do - I signal it (using a boolean within the AsyncTask) it should stop. Then set a timer to re-test the AsyncTask within 10 mSec, to see if it terminated, and start the new search.

  • Is this a smart method ? Or is there another approach you would take ?

TIA


First yes, AsyncTask is a good way to do this. The problem I see with your approach is the timer waiting to watch something die. When you invoke the asyncTask hold onto a reference of it. Let it keep state for you so you know if it's out searching or it's has returned. When the user clicks another letter you can tell that asyncTask to cancel. Something like this:

public void onClick() {
   if( searchTask != null ) {
      searchTask.cancel();
   }

   searchTask = new SearchTask( MyActivity.this ).execute( textInput.getText() );
}

public class SearchTask extends AsyncTask<String,Integer,List<SearchResult>> {
    private boolean canceled = false;

    protected onPostExecute( List<SearchResult> results ) {
       if( !canceled ) {
          activity.handleResults( results );
       }
    }

    public void cancel() {
       canceled = true;
    }
}

This is safe because onPostExecute() is on the UI thread. And cancel() is only called from the UI thread so there is no thread safety issues, and no need to synchronize. You don't have to watch a thread die. Just let the GC handle cleaning up. Once you drop the reference to the AsyncTask it will just get cleaned up. If your AsyncTask blocks that's ok because it only hangs up the background thread, and when the timeout hits it will resume by calling onPostExecute(). This also keeps your resources to a minimum without using a Timer.

Things to consider about this approach. Sending a new request everytime a new letter is typed can overload your servers because the first few letters are going to produce the largest search results. Either limit the number of results you'll return from the server (say 10-50 results max), or wait until they've entered enough characters to keep results down (say 3). The cons of making the user type more characters is the feedback doesn't kick in until 3 chars. However, the pro is it will dramatically reduce the hits on your server.

0

精彩评论

暂无评论...
验证码 换一张
取 消