0

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;
user3807877
  • 123
  • 1
  • 8

1 Answers1

1

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.

nburk
  • 22,409
  • 18
  • 87
  • 132
  • I never used performSelectorInBackground. could you please write an example on how to do it? – user3807877 Nov 09 '14 at 16:36
  • say your method is called `-(void)saveContacts`, then you can call `[self performSelectorInBackground:@selector(saveContacts) withObject:nil];` – nburk Nov 09 '14 at 18:30