I need to get the number of all contacts on a user's device. The deprecation message on ABAddressBookGetPersonCount says:
use count of fetch results for CNContactFetchRequest with predicate = nil
Here is what I made up following that guidance:
__block NSUInteger contactsCount = 0;
NSError *error;
CNContactFetchRequest *request = [[CNContactFetchRequest alloc] initWithKeysToFetch:@[CNContactGivenNameKey]];
BOOL success = [self.contactStore enumerateContactsWithFetchRequest:request error:&error
usingBlock:^(CNContact * _Nonnull contact, BOOL * _Nonnull stop) {
contactsCount += 1;
}];
if (!success || error) {
NSLog(@"error counting all contacts, error - %@", error.localizedDescription);
}
However this looks terrible in terms of performance. I have not found another way of getting the count without enumerating CNContact objects. Am I missing something?
Thank you in advance!