【发布时间】:2012-03-19 16:36:44
【问题描述】:
当用户搜索时,我想根据地址簿数据搜索该查询,以获取姓名、电话号码、网址、电子邮件地址等任何内容。我的解决方案运行良好,但速度很慢。如果通讯录数据很大,应用就会卡住。如何优化搜索,让我的应用程序即使在通讯录数据很大时也不会挂起?
这是我的代码
- (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText{
[self takeSomeActionWhenTextChange];
}
-(void)takeSomeActionWhenTextChange{
[contactArray removeAllObjects];
NSString *searchText=[[textSearchBar text] lowercaseString];
for (int index=0; index<count; index++) {
ABRecordRef record=CFArrayGetValueAtIndex(people, index);
//[self checkStringISAddress:searchText withRecord:record];
if ([self checkStringIsFirstName:searchText withRecord:record]==YES
|| [self checkStringIsLastName:searchText withRecord:record] == YES
||[self checkStringIsNote:searchText withRecord:record]==YES
|| [self checkStringIsAddress:searchText withRecord:record]==YES
|| [self checkStringIsCompany:searchText withRecord:record]==YES
||[self checkStringIsEmail:searchText withRecord:record]
||[self checkStringIsPhonenumber:searchText withRecord:record]==YES )
{
NSLog(@"object added inside Array");
[contactArray addObject:record];
[contactTableView reloadData];
}else{
NSLog(@"No Match For this object");
[contactTableView reloadData];
}
}
}
我将检查搜索查询中的子字符串是否与名字、姓氏、电子邮件等匹配。上述方法包含检查子字符串是否存在的逻辑?如果 ti 匹配,我会将其添加到数组中,否则不添加。
我应该使用线程还是 GCD 来执行搜索?如果是,如何?如何更新我的表格视图?
【问题讨论】:
标签: iphone objective-c addressbook