1

我有一些代码可以从 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];
4

2 回答 2

3

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

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

每个附加看起来像这样:

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

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

于 2012-07-06T05:55:34.987 回答
1

使用 aNSMutableString并使用该方法构建您的 XML appendFormat:。在您的循环中,您正在复制到目前为止多次组装的整个字符串。

于 2012-07-06T05:57:07.547 回答