【发布时间】:2012-01-08 20:56:06
【问题描述】:
我正在编写一个应用程序,我需要读取地址簿数据以搜索一些感兴趣的联系人,这类似于现在许多应用程序(如 Viber、Whatsapp、Tango ......)。我需要进行匹配,因此我将数据发送到服务器并回复客户端,哪些联系人在他们的设备上安装了相同的应用程序。
这个idea的逻辑和机制我没有问题,我的问题是速度!我能够做我想做的事,但这个过程需要 27 秒才能在 iPhone4 上完成,上面有 500 个联系人。在同一台设备上,如果我们尝试 Viber 或 Whatsapp(或任何类似的应用程序),该过程只需不到 5 秒。
我的方法非常简单,我做一个 for 循环并阅读所有内容。我怎样才能像其他应用一样做同样的事情但速度更快?
这是我使用的代码:
//variable definitions
ABAddressBookRef addressBook = ABAddressBookCreate();
CFArrayRef people = ABAddressBookCopyArrayOfAllPeople(addressBook);
CFMutableArrayRef peopleMutable = CFArrayCreateMutableCopy(kCFAllocatorDefault, CFArrayGetCount(people), people);
//sort the contents of the Mutable Array
CFArraySortValues(peopleMutable, CFRangeMake(0, CFArrayGetCount(peopleMutable)), (CFComparatorFunction) ABPersonComparePeopleByName, (void*) ABPersonGetSortOrdering());
//read the Address Book
NSString *fullName, *number;
ABRecordRef record = ABPersonCreate();
ABMutableMultiValueRef multi;
int contactID;
int nameCount=0;//used to count the names in the string to send to server
NSMutableString *strNamesToSend = [[NSMutableString alloc] init];
for(CFIndex i=0; i< CFArrayGetCount(people); i++)
{
record = CFArrayGetValueAtIndex(people, i);
multi = ABRecordCopyValue(record, kABPersonPhoneProperty);
//Contact ID
contactID = (int)ABRecordGetRecordID(record);
//Full Name
fullName = [NSString stringWithFormat:@"%@ %@", (NSString *)ABRecordCopyValue(record, kABPersonFirstNameProperty), (NSString *)ABRecordCopyValue(record, kABPersonLastNameProperty)];
fullName = [fullName stringByReplacingOccurrencesOfString:@" (null)" withString:@""];
//fill data into AddressBook Table
if(dbOpen == SQLITE_OK)
{
//pure sqlite3 work to save the names in my app
}
//Get multiple numbers from each user (if any)
for(CFIndex j=0; j<ABMultiValueGetCount(multi); j++)
{
number = (NSString *)ABMultiValueCopyValueAtIndex(multi, j);
nameCount++;
//fill data into AllNumbers Table
if(dbOpen == SQLITE_OK)
{
//another sqlite3 work to save the numbers
}
}
//send to the server every 29 numbers so we don't send all the 500 numbers at once
if(nameCount > 29)
{
//send to server
}
【问题讨论】:
-
您是否对它进行了分析以查看这些时间都花在了哪里?您是否将数据同步发送到服务器?
标签: iphone ios client-server addressbook