Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Best Practices for Handling Search

I've got a SearchView setup, and I have a loosely decoupled architecture using Retrofit and Otto.

I am wondering what the Best Practices are for Search in Android, or any mobile application in general (meaning something like this could be applied to iOS as well).

Specifically I am using an AutoCompleteTextView to handle suggestions in my SearchView and the data is coming straight from the API. I don't believe this is a best practice since everytime a user changes text in the SearchView there is an API call initiated.

I'm thinking about storing a Cache in SQLite and then pinging results from there, but what if the user wants the most immediate data? How would you handle that? What pattern would that employ?

Would appreciate any thoughts on the best architecture or approach to Search in Android.

like image 708
AndyRoid Avatar asked Jul 07 '15 18:07

AndyRoid


1 Answers

I think there is no sense to make an API call before user stopped typing. So, you can put a delay, say, 500ms. When user stops typing, after 500ms you make an API call and show the results.

You can use a Handler's postDelayed method for scheduling search API calls. You can use Handler to control the message queue. You post a delayed Runnable each time user types a character and cancel previous messages. It will look this way:

public void onTextChanged(CharSequence s, int start, int before, int count) {
    handler.removeCallbacks(searchRunnable);
    handler.postDelayed(searchRunnable, 500);
}
like image 193
Gennadii Saprykin Avatar answered Oct 04 '22 05:10

Gennadii Saprykin