当用户搜索姓名,电话号码,网址,电子邮件地址等内容时,我想根据地址簿数据搜索该查询。我的解决方案工作正常,但速度很慢。如果地址簿数据很大,应用程序就会卡住。如何优化搜索,以便我的应用程序即使在大型地址簿数据中也不会挂起?
编辑1这是我的代码
- (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];
}
}
}
我将检查来自搜索查询的子字符串是否与名字,姓氏,电子邮件和&amp;等等。上面的方法包含检查子串是否存在的逻辑?如果ti匹配,我会将它添加到数组中,否则不会。
我应该使用线程或GCD进行PERFOEM搜索吗?如果是的,怎么样?我如何更新我的表格视图?
答案 0 :(得分:-2)
我有一个可能会帮助你
1.获取所有联系人
NSArray *thePeoples = (NSArray*)ABAddressBookCopyArrayOfAllPeople(addressBook);
2.从contacts数组(thePeoples)
创建另一个数组(记录)records:{name:"abc", number:"123", url:"url", email:"email"}
3.使用谓词
搜索mutableArray(记录)NSPredicate * myPredicate = [NSPredicate predicateWithFormat:@"record.phoneNumber contains %@",string];
NSArray * filteredArray = [records filteredArrayUsingPredicate:myPredicate];
希望它能帮到你