0

using an iPhone 6 running iOS 8.2

I added a gmail account via the os settings, and the contacts (~350) for gmail show up in my contact list.

I also added 3 new contacts directly via the phone.

If I try to programtically retrieve all contacts via objective-c they all come back

ABAddressBookRef addressBookRef = ABAddressBookCreateWithOptions(NULL, nil);
NSArray *allContacts = (__bridge_transfer NSArray *)ABAddressBookCopyArrayOfAllPeople(addressBookRef);

If I try to get them with a sort order [via this question], only the 3 Records I added via the phone show up. The gmail ones do not return.

ABAddressBookRef addressBookRef = ABAddressBookCreateWithOptions(NULL, nil);
ABRecordRef source = ABAddressBookCopyDefaultSource(addressBookRef);
NSArray *sortedContacts = (__bridge_transfer NSArray *)ABAddressBookCopyArrayOfAllPeopleInSourceWithSortOrdering(addressBookRef, source, kABPersonSortByFirstName);

Edit:It does not matter if i sort by first name or last name, the same 3 records come back.

Am I doing something incorrect, or do I need to somehow trigger an index on the gmail Records?

Edit 2: _bridge to __bridge_transfer

Community
  • 1
  • 1
MattoTodd
  • 14,467
  • 16
  • 59
  • 76

1 Answers1

1

Your three iOS contacts and your gmail contacts are undoubtedly different sources. Your second code sample effectively says "I don't care about the gmail source, just get the contacts from the default source".

In ABAddressBookCopyArrayOfAllPeopleInSourceWithSortOrdering, if you pass NULL for the source, it gets all of the contacts. I've never found documentation of this behavior, though, so I always retrieve all the contacts via ABAddressBookCopyArrayOfAllPeople and then sort them myself.

Rob
  • 415,655
  • 72
  • 787
  • 1,044
  • any performance difference for you sorting vs the native api? – MattoTodd Apr 10 '15 at 16:10
  • A quick benchmark suggest that with ~3,300 contacts on iPhone 6+ running iOS 8.3, it took 21 msec using built in sort and 34 msec manually sorting them. So it looks like native API is marginally faster, but not observably so in practical applications. – Rob Apr 10 '15 at 16:42