meetpd
meetpd

Reputation: 9219

How to increase search speed in Iphone SDK

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!

Upvotes: 3

Views: 618

Answers (3)

mozillanerd
mozillanerd

Reputation: 560

I don't know whether your table is indexed. If not, you should create an index for your table. Update of the table would be slower, but the search should be faster. Good luck.

Upvotes: 0

Ole Begemann
Ole Begemann

Reputation: 135548

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: and performSelectorOnMainThread:withObject:waitUntilDone:. From the search bar delegate method, try calling something like:

 // -searchForString: is our search method and searchTerm is the string we are searching for
 [self performSelectorInBackground:@selector(searchForString:) withObject:searchTerm];

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:

- (void)searchForString:(NSString *)searchTerm
{
    // First create an autorelease pool (we must do this because we are on a new thread)
    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];

    // Perform the search as you normally would
    // The result should be an array containing your search results
    NSArray *searchResults = ...

    // Pass the search results over to the main thread
    [self performSelectorOnMainThread:@selector(searchDidFinishWithResult:) withObject:searchResults waitUntilDone:YES];

    // Drain the ARP
    [pool drain];
}

Now, the custom method searchDidFinishWithResult: is responsible for updating the UI with the search results:

- (void)searchDidFinishWithResult:(NSArray *)searchResult
{
    // Update 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 (NSOperation might be better in that case).

Upvotes: 6

anon
anon

Reputation:

Rather than searching your whole list every time "textDidChange" gets called, could you search it only when "searchBarSearchButtonClicked" gets called instead?

You'd loose the auto-update-as-they-type, but it wouldn't create the delay you're seeing each time.

Upvotes: 1

Related Questions