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

标签 iphone ios client-server addressbook

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

我的idea逻辑和机制都没有问题,我的问题是速度!我能够做我想做的事,但在有 500 个联系人的 iPhone4 上完成这个过程需要 27 秒。在同一台设备上,如果我们尝试 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
        }

最佳答案

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

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

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

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

我注意到 nameCount 没有被重置,这意味着一旦它达到 29,你的 if case 每次都会被命中,导致大量的网络请求。

关于iphone - 如何像其他应用程序(如 Viber、Whatsapp...)一样快速阅读地址簿,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8781251/

相关文章:

iphone - 如何为 UITableView 创建详细 View 以允许我编辑 Core Data 中的数据?

ios - 如何在 View 上应用渐变模糊效果?

iphone - 通过 TBXML 解析 XML 在演示项目中有效,但在我的真实项目中不起作用

ios - 将自定义单元格内的 View 居中

ios - 如何为自动布局创建自定义类?

ios - 当我尝试加载 AdMob 广告时 iPad 崩溃

python - 终极吉他API和终极API的使用

iphone - 用于将用户发送至设备应用商店上的应用评论页面的 URL

language-agnostic - 实时服务器中的 fatal error

javascript - ASP.NET:在运行时注入(inject) Javascript(启用 AJAX)