iPhone-移动本地搜索优化

发布于 2024-12-22 16:59:13 字数 1614 浏览 0 评论 0原文

我正在iPhone应用程序中实现移动本地搜索:

    - (void)searchBar:(UISearchBar *)theSearchBar textDidChange:(NSString *)searchText {

        //Remove all objects first.
        [copyListOfItems removeAllObjects];

        if([searchText length] > 0) {

            [ovController.view removeFromSuperview];
            searching = YES;
            letUserSelectRow = YES;
            self.tableView.scrollEnabled = YES;
            [self searchTableView];
        }
        else {

            [self.tableView insertSubview:ovController.view aboveSubview:self.parentViewController.view];

            searching = NO;
            letUserSelectRow = NO;
            self.tableView.scrollEnabled = NO;
        }

        [self.tableView reloadData];
    }



    - (void) searchTableView {

        NSString *searchText = searchBar.text;
        NSMutableArray *searchArray = [[NSMutableArray alloc] init];

        for (NSDictionary *dictionary in listOfItems)
        {
            NSArray *array = [dictionary objectForKey:@"key_pdescription"];
            [searchArray addObjectsFromArray:array];
        }

//in requirement searchArray has more than 50k objects
        for (NSString *sTemp in searchArray)
        {
            NSRange titleResultsRange = [sTemp rangeOfString:searchText options:NSCaseInsensitiveSearch];

            if (titleResultsRange.length > 0)
                [copyListOfItems addObject:sTemp];
        }

        [searchArray release];
        searchArray = nil;
    }

它对于数百条记录运行良好,但我必须实现5万条或更多记录,输入1个字符后搜索速度非常慢,我必须等待输入下一个字符。 我想如果有人为移动本地搜索实现了一些优化算法,请指导我如何继续。

I am implementing mobile local search in iPhone application:

    - (void)searchBar:(UISearchBar *)theSearchBar textDidChange:(NSString *)searchText {

        //Remove all objects first.
        [copyListOfItems removeAllObjects];

        if([searchText length] > 0) {

            [ovController.view removeFromSuperview];
            searching = YES;
            letUserSelectRow = YES;
            self.tableView.scrollEnabled = YES;
            [self searchTableView];
        }
        else {

            [self.tableView insertSubview:ovController.view aboveSubview:self.parentViewController.view];

            searching = NO;
            letUserSelectRow = NO;
            self.tableView.scrollEnabled = NO;
        }

        [self.tableView reloadData];
    }



    - (void) searchTableView {

        NSString *searchText = searchBar.text;
        NSMutableArray *searchArray = [[NSMutableArray alloc] init];

        for (NSDictionary *dictionary in listOfItems)
        {
            NSArray *array = [dictionary objectForKey:@"key_pdescription"];
            [searchArray addObjectsFromArray:array];
        }

//in requirement searchArray has more than 50k objects
        for (NSString *sTemp in searchArray)
        {
            NSRange titleResultsRange = [sTemp rangeOfString:searchText options:NSCaseInsensitiveSearch];

            if (titleResultsRange.length > 0)
                [copyListOfItems addObject:sTemp];
        }

        [searchArray release];
        searchArray = nil;
    }

It's working fine for hundreds of records,but I have to implement for 50 thousands or more than that,that search is very very slow after typing 1 character I have to wait to enter next character.
I want to if someone has implemented some optimized algorithm for mobile local search,please guide me how to proceed.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

乙白 2024-12-29 16:59:13

您可以使用核心数据并限制获取结果并在搜索结果屏幕上填充数据,并且可以为下一组记录实现分页。

NSFetchRequest *request = [[NSFetchRequest alloc] init];
//[request setEntity:entity];
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"predicateformart"];
[request setPredicate:predicate];
[request setFetchLimit:100];//100 records for example

you can use core data and limit the fetch result and populate the data on search result screen,and you can implementat pagination for the next set of records.

NSFetchRequest *request = [[NSFetchRequest alloc] init];
//[request setEntity:entity];
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"predicateformart"];
[request setPredicate:predicate];
[request setFetchLimit:100];//100 records for example
泪冰清 2024-12-29 16:59:13

问题出在 searchTableView 方法上。
首先:不要每次搜索时都创建searchArray,更好的选择是在viewDidLoad上构建它。
其次:使用

[NSArray filteredArrayUsingPredicate:(NSPredicate *)predicate] 

而不是手动比较。

The problem lies in the method searchTableView.
Firstly: do not create searchArray every time you search, better choice is build it on viewDidLoad.
Secondly: use

[NSArray filteredArrayUsingPredicate:(NSPredicate *)predicate] 

instead of manual comparing.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文