I am creating a lookup table mapping contact phone numbers to their corresponding ABRecordRef (I need this so I can efficiently look up contact names and photos based on the phone number a user has dialed).
Unfortunately, for 500 contacts it takes around 4 seconds to loop through all the contacts and create my lookup table, which makes my application load much too slowly.
Has anybody else seen problems like this or have any suggested workarounds?
I am testing on a 3G iPhone running OS V3.0. The project is built for target 'Device 2.1'.
Here is the code:
ABAddressBookRef lAddressBook = ABAddressBookCreate();
CFArrayRef lRawAddressBookEntries =
ABAddressBookCopyArrayOfAllPeople(lAddressBook);
static NSMutableDictionary sCustomAddressBookPersonRefs =
[[NSMutableDictionary alloc] initWithCapacity:1000];
CFIndex lTotalContactsCount = ABAddressBookGetPersonCount(lAddressBook);
/*************************************************************************/
/* Loop through all the contacts storing a pointer to the address book */
/* entry for each phone number. */
/*************************************************************************/
for (CFIndex i = 0; i < lTotalContactsCount; i++)
{
ABRecordRef lRef = CFArrayGetValueAtIndex(lRawAddressBookEntries, i);
ABMultiValueRef lPhoneNumbers = ABRecordCopyValue(lRef,
kABPersonPhoneProperty);
CFIndex lContactPhoneNumberCount = ABMultiValueGetCount(lPhoneNumbers);
/***********************************************************************/
/* Loop through all the phone numbers available for this contact. */
/***********************************************************************/
for (int j = 0; j < lContactPhoneNumberCount; j++)
{
/*********************************************************************/
/* Get the next phone number and remove the formatting. */
/*********************************************************************/
CFStringRef lPhoneNumber =
ABMultiValueCopyValueAtIndex(lPhoneNumbers, j);
[sCustomAddressBookPersonRefs setValue:(id)lRef
forKey:(NSString *)lPhoneNumber];
CFRelease(lPhoneNumber);
}
CFRelease(lRef);
CFRelease(lPhoneNumbers);
}
CFRelease(lRawAddressBookEntries);
The first part of the code before the for loop only takes 0.2 to 0.75 secs to run (including the ABAddressBookCopyArrayOfAllPeople).
There was only one NSLog line in the outer for loop initially, but I preprocessed that out before testing on the Device.
After seeing the problem I put NSLog lines between every line of code and none of the lines seemed to cause significantly longer delays than others. With all those trace lines in it takes ~50 secs to create the lookup table, and each line takes about 0.01 second to output, with the occasional 0.1 to 0.2 secs delay (not the same line of code each time).
Any ideas would be appreciated!
I'm pretty confident no memory management mistakes are present as I've run the Leaks checker. There also does not appear to be any way to lookup a contact individually based on its phone number.
(2nd Nov 2009) I have now raised bugs with Apple for this issue:
-Bug ID# 7357996 (Performance) - ABAddressBook SDK APIs have terrible performance
-Bug ID# 7357980 (Enhancement) - ABAddressBook provides no way to lookup contact based on phone number