iPhone-移动本地搜索优化
我正在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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以使用核心数据并限制获取结果并在搜索结果屏幕上填充数据,并且可以为下一组记录实现分页。
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.
问题出在 searchTableView 方法上。
首先:不要每次搜索时都创建searchArray,更好的选择是在viewDidLoad上构建它。
其次:使用
而不是手动比较。
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
instead of manual comparing.