My iphone App shows a table view having list of 6000 items. (these items are in SQLite file)
User can search these items. BUT, when I click Search bar & start typing the first letter, it takes ages before I can type in the second letter. Similarly it takes long time to type each letter before I could begin searching.
Is there a way to increase the typing speed of the search toolbar so that user can quickly type in 5-6 letters for searching?
I appreciate your help.
Thanks!
If your search is too slow and therefore blocks the UI, you should perform the search asynchronously so as not to block the main thread. To do this, there are many options, including Grand Central Dispatch (4.0+),
NSOperation,performSelectorInBackground:.... The best approach for you depends on the architecture of your app/algorithm and what you’re most comfortable with.Edit: to start, read the documentation for
performSelectorInBackground:withObject:andperformSelectorOnMainThread:withObject:waitUntilDone:. From the search bar delegate method, try calling something like:Now Cocoa will create a background thread and call your custom
-searchForString:method on that thread. That way, the main thread will not be blocked. The custom method should look something like this:Now, the custom method
searchDidFinishWithResult:is responsible for updating the UI with the search results:This is probably the easiest approach for a start. The solution is not complete yet, partly because search tasks will pile up if the users types faster than a search can complete. You should perhaps incorporate an idle timer that waits a while until a search is fired off or you would need to cancel an ongoing search task (
NSOperationmight be better in that case).