user3807877
user3807877

Reputation: 123

ABAddressBookCopyArrayOfAllPeople Slow

I am writing an app where I need to read the user’s address book and display a list of all his contacts. The iPhone I’m testing with has ~ 100 contacts and it takes really much time to load the contacts.

ABAddressBookRef addressBook = ABAddressBookCreateWithOptions(NULL, NULL);
NSArray *allContacts = (__bridge NSArray *)ABAddressBookCopyArrayOfAllPeople(addressBook);
ABMultiValueRef phones = NULL;
ABRecordRef person = NULL;
for (int i =0; i < allContacts.count; i++) {
    person = (__bridge ABRecordRef)([allContacts objectAtIndex:i]);
    if (person != nil) {
        phones = ABRecordCopyValue(person, kABPersonPhoneProperty);
        if (ABMultiValueGetCount(phones) == 0) {
            CFErrorRef error = nil;
            ABAddressBookRemoveRecord(addressBook, person, &error);
        }
        CFRelease(phones);
    }
}
CFErrorRef saveError = nil;
ABAddressBookSave(addressBook, &saveError);

ABPeoplePickerNavigationController *picker = [[ABPeoplePickerNavigationController alloc] init];
picker.view.backgroundColor=[UIColor clearColor];
picker.peoplePickerDelegate = self;
picker.delegate=self;
NSArray *displayedItems =
[NSArray arrayWithObject:[NSNumber
                          numberWithInt:kABPersonPhoneProperty]];
picker.displayedProperties = displayedItems;

Upvotes: 0

Views: 327

Answers (1)

nburk
nburk

Reputation: 22731

You can perform the copying in a background thread using performSelectorInBackground:withObject:, that way it won't affect the main thread and you don't have to wait in the UI.

Upvotes: 1

Related Questions