views:

864

answers:

2

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

+6  A: 

If you cannot optimize the routinue, you can also fork a new thread for the loading, then the app can continue to load and be responsive for the user.

[NSThread detachNewThreadSelector:@selector(_loadContactsInAnotherThread:) toTarget:self withObject:self];

-(void)loadContactsInAnotherThread:(void *)obj
{
     NSLog("Do time intensive stuff here.");
}
NWCoder
Thanks, this seems to work nicely. I'm sure I'll have some concurrency bugs to iron out, but it's looking good for now...
Dan J
Thanks a lot for both the code and the answer!
Martin
+1  A: 

There's really no answer to this. I have a dialer type of app on the AppStore and my app suffers from the same problem. The AB API Is really limiting in terms of contacts query. The best you can do is to code in some animation to make the wait less painful.

erotsppa