ABAddressBookCopyArrayOfAllPeople慢

时间:2014-11-09 15:31:57

标签: ios iphone xcode abaddressbook

我正在编写一个应用程序,我需要阅读用户的地址簿并显示所有联系人的列表。我正在测试的iPhone有大约100个联系人,加载联系人需要很长时间。

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;

1 个答案:

答案 0 :(得分:1)

您可以使用performSelectorInBackground:withObject:在后​​台线程中执行复制,这样就不会影响主线程,也不必在UI中等待。