繁体   English   中英

优化Objective C代码以访问联系人

[英]Optimize Objective C code to access contacts

我有一些代码来复制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]引用 )代替,可以更好地利用内存并获得更好的性能。 这将修改现有对象,分配更多内存以接受新字符串。

每个追加都是这样的:

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

它仍会创建大量自动释放的对象,但它们小得多。

使用NSMutableString并使用appendFormat:方法构建XML。 在你的循环中,你复制了迄今为止已经多次组装的整个字符串。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM