如何提高Iphone SDK的搜索速度?

时间:2022-06-01 22:00:21

My iphone App shows a table view having list of 6000 items. (these items are in SQLite file)

我的iphone应用程序显示了一个包含6000个条目的表视图。(这些项目在SQLite文件中)

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?

有没有办法提高搜索工具栏的打字速度,让用户可以快速输入5-6个字母进行搜索?

I appreciate your help. Thanks!

我很感激你的帮助。谢谢!

3 个解决方案

#1


6  

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.

如果搜索太慢,从而阻塞UI,那么应该异步执行搜索,以免阻塞主线程。要做到这一点,有很多选择,包括*调度(4.0 +),NSOperation,performSelectorInBackground:....对你来说,最好的方法取决于你的应用程序/算法的架构,以及你最喜欢什么。

Edit: to start, read the documentation for performSelectorInBackground:withObject: and performSelectorOnMainThread:withObject:waitUntilDone:. From the search bar delegate method, try calling something like:

编辑:首先,阅读performSelectorInBackground:withObject:和performSelectorOnMainThread:withObject:waitUntilDone:的文档。在search bar委托方法中,尝试调用如下内容:

 // -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:

现在,Cocoa将创建一个后台线程,并在该线程上调用你的自定义-searchForString:方法。这样,主线程就不会被阻塞。自定义方法应该如下所示:

- (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:

现在,自定义方法searchDidFinishWithResult:负责更新UI与搜索结果:

- (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).

这可能是最简单的方法。解决方案还没有完成,部分原因是,如果用户键入的速度超过搜索的完成速度,搜索任务就会堆积起来。您可能应该合并一个空闲计时器,它会等待一段时间,直到搜索被触发,或者您需要取消正在进行的搜索任务(在这种情况下,NSOperation可能更好)。

#2


1  

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

与其每次“textDidChange”被调用时都搜索整个列表,不如只在“searchbarsearchbuttonclick”被调用时搜索它?

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

您可能会失去自动更新作为y类型,但它不会造成每次看到的延迟。

#3


0  

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.

我不知道您的表是否被索引。如果不是,您应该为您的表创建一个索引。更新表会比较慢,但是搜索应该会更快。祝你好运。

#1


6  

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.

如果搜索太慢,从而阻塞UI,那么应该异步执行搜索,以免阻塞主线程。要做到这一点,有很多选择,包括*调度(4.0 +),NSOperation,performSelectorInBackground:....对你来说,最好的方法取决于你的应用程序/算法的架构,以及你最喜欢什么。

Edit: to start, read the documentation for performSelectorInBackground:withObject: and performSelectorOnMainThread:withObject:waitUntilDone:. From the search bar delegate method, try calling something like:

编辑:首先,阅读performSelectorInBackground:withObject:和performSelectorOnMainThread:withObject:waitUntilDone:的文档。在search bar委托方法中,尝试调用如下内容:

 // -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:

现在,Cocoa将创建一个后台线程,并在该线程上调用你的自定义-searchForString:方法。这样,主线程就不会被阻塞。自定义方法应该如下所示:

- (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:

现在,自定义方法searchDidFinishWithResult:负责更新UI与搜索结果:

- (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).

这可能是最简单的方法。解决方案还没有完成,部分原因是,如果用户键入的速度超过搜索的完成速度,搜索任务就会堆积起来。您可能应该合并一个空闲计时器,它会等待一段时间,直到搜索被触发,或者您需要取消正在进行的搜索任务(在这种情况下,NSOperation可能更好)。

#2


1  

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

与其每次“textDidChange”被调用时都搜索整个列表,不如只在“searchbarsearchbuttonclick”被调用时搜索它?

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

您可能会失去自动更新作为y类型,但它不会造成每次看到的延迟。

#3


0  

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.

我不知道您的表是否被索引。如果不是,您应该为您的表创建一个索引。更新表会比较慢,但是搜索应该会更快。祝你好运。