如何像其他应用程序一样快速阅读地址簿(如Viber,Whatsapp ......)

时间:2012-01-08 20:56:06

标签: iphone ios client-server addressbook

我正在编写一个应用程序,我需要阅读地址簿数据以搜索一些感兴趣的联系人,类似于现在许多应用程序所做的事情(如Viber,Whatsapp,Tango ......)。我需要进行匹配,因此我将数据发送到服务器并回复客户端,其设备上安装了相同的应用程序。

我的想法的逻辑或机制没有问题,我的问题是速度!我能够做我想要的但是这个过程需要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
        }

1 个答案:

答案 0 :(得分:5)

您是否尝试过分析代码?分析器应该能够快速识别代码的缓慢部分。

从非常简短的检查中,我注意到你在每次迭代时计算数组大小而不是一次。将它移出你的循环:

int count = CFArrayGetCount(people);
for (CFIndex i = 0; i < count; i++)

您没有详细说明您正在进行的SQL调用,但是您正在检查SQLITE_OK这一事实意味着您每次都要在循环中打开数据库。如果是这种情况,您应该将此调用移出循环,而不是每次都打开数据库。

我注意到nameCount没有被重置,这意味着一旦它达到29,你的if案例将每次都被点击,导致大量的网络请求。