iphone - 优化 objective-c 代码以访问联系人

标签 iphone objective-c ios xcode addressbook

我有一些代码可以从Addreesbook复制联系人。如果联系人数量少,它会完美地工作。现在,在我的手机中,我们有1200个联系人,当我尝试复制它们时,应用程序崩溃了。谁能帮助我优化此代码或重写代码?我使用的代码添加如下:

ABAddressBookRef addressBook = ABAddressBookCreate();
    CFArrayRef allPeople = ABAddressBookCopyArrayOfAllPeople(addressBook);
    CFIndex nPeople = ABAddressBookGetPersonCount(addressBook);

    NSString *requestContactsString = @"<contacts>";    


    for (int i=0; i<nPeople; i++)
    {
        NSLog(@"Started : %d", i);

        ABRecordRef ref = CFArrayGetValueAtIndex(allPeople, i);
        CFTypeRef firstName = ABRecordCopyValue(ref, kABPersonFirstNameProperty);
        CFTypeRef lastName = ABRecordCopyValue(ref, kABPersonLastNameProperty);
        CFTypeRef email = ABRecordCopyValue(ref, kABPersonEmailProperty);
        CFTypeRef phone = ABRecordCopyValue(ref, kABPersonPhoneProperty);

        requestContactsString = [requestContactsString stringByAppendingFormat:@"<item>"];

        if(firstName)
        {
            requestContactsString = [requestContactsString stringByAppendingFormat:@"<firstname>%@</firstname>", firstName];
            CFRelease(firstName);
            firstName = nil;
        }
        if(lastName)
        {
            requestContactsString = [requestContactsString stringByAppendingFormat:@"<lastname>%@</lastname>", lastName];
            CFRelease(lastName);
            lastName = nil;
        }
        if(email)
        {
            if(ABMultiValueGetCount(email)>0)
            {
                CFTypeRef em = ABMultiValueCopyValueAtIndex(email, 0);
                requestContactsString = [requestContactsString stringByAppendingFormat:@"<email>%@</email>", em];
                CFRelease(em);
            }
            CFRelease(email);
            email = nil;
        }
        if(phone)
        {
            if(ABMultiValueGetCount(phone)>0)
            {
                CFTypeRef ph = ABMultiValueCopyValueAtIndex(phone, 0);
                requestContactsString = [requestContactsString stringByAppendingFormat:@"<phone>%@</phone>", ph];
                CFRelease(ph);
            }
            CFRelease(phone);
            phone = nil;
        }

        requestContactsString = [requestContactsString stringByAppendingFormat:@"</item>"];
    }


    if(allPeople)
    {
        CFRelease(allPeople);
        allPeople = nil;
    }
    if(addressBook)
    {
        CFRelease(addressBook);
        addressBook = nil;
    }

    requestContactsString = [requestContactsString stringByAppendingFormat:@"</contacts>"];

    NSString *hashedContactsString = [self generateHashedPassword:requestContactsString];

最佳答案

我看到的主要低效率是使用[NSString stringByAppendingFormat],每次调用它时都会创建一个新的NSString对象。这意味着您有大量的自动释放的NSString对象,这些对象将在下一个运行循环之前不再使用(除非您使用的是ARC,在这种情况下情况可能会更好)。

我认为您可以通过将requestContactsString设置为NSMutableString并使用[NSMutableString appendString](reference)来更好地利用内存并获得更好的性能。这将修改现有对象,分配更多内存以接受新字符串。

每个追加看起来像这样:

[requestContactsString appendString:[NSString stringWithFormat:@"<lastname>%@</lastname>", lastName]];

仍然会创建大量自动释放的对象,但是它们的小很多

关于iphone - 优化 objective-c 代码以访问联系人,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11356532/

相关文章:

ios - 为什么要使用委托(delegate)?

ios - NSData 到 NSArray;数组为空

objective-c - 如何获取在 CTFrame 上绘制的文本的实际高度

ios - 如何重新发送电子邮件验证 parse.com

ios - 添加绝对位置按钮

javascript - watchPosition() 与 getCurrentPosition() 与 setTimeout

iphone - 如何对 CVImageBufferRef 视频帧进行操作

iphone - 从来自网络服务的位置创建多个 MKOverlays 折线

ios - 在代码中更改纵横比值

ios - UISearchController 与 UITableView?