getContactsAsync not returning all contacts

I am using the following to get all contacts, and it works well except only returns the first 100 or so contacts. They are in alphabetical order and it only makes it to letter C. Any idea what may be causing this? The pageSize of 0 should return all contacts. Thanks in advance.

    const contacts = await Expo.Contacts.getContactsAsync({
      fields: [Expo.Contacts.PHONE_NUMBERS, Expo.Contacts.EMAILS],
      pageSize: 0,
      pageOffset: 0,
      sort: sortType
    });

Hey @greg_mtl,

Is this occurring on both iOS and Android? Also, what SDK version are you running? Lastly, could you throw a Snack together that reproduces this issue so we can try and test it on our end?

Cheers,

Adam

Thanks for the reply. It occurs for iOS. I cannot test it for Android at the moment. It is SDK 30.

Sure, here is the component from which the user clicks a button to import from contacts:

And this data is then loaded into a new component which lists all the returned contacts from which the user can choose one:

I wasn’t able to make this run in your online emulator, but the code is exactly what I am using and it works well, except for the aforementioned issue of only loading about 50-100 contacts. That partial list of contacts loads pretty fast, and is able to scroll, but tapping a contact doesn’t do anything for about a minute, when that tap finally gets registered and loads the next screen (where the user chooses what info they want to import from the contact).

If I change pageSize: 0 to pageSize: 10, to load only 10 instead of all contacts, then it all works very well and very fast.

Note that the list sometimes stops at letter C, sometimes letter B, so it doesn’t appear that there’s a culprit Contact causing this issue.

Hopefully this helps. Thanks again.

In case it helps, when I run the commented lines below (instead of the Actions line that loads the new component) all the contacts are loaded in the console very quickly, and the app doesn’t freeze:

    const sortType = Expo.Contacts.SortTypes.LastName;

    const contacts = await Expo.Contacts.getContactsAsync({
      fields: [Expo.Contacts.PHONE_NUMBERS, Expo.Contacts.EMAILS],
      pageSize: 0,
      pageOffset: 0,
      sort: sortType
    });

    if (contacts.total > 0) {
      // contacts.data.map((contact) => {
      //   console.log('contact=');
      //   console.log(JSON.stringify(contact));
      // });
      Actions.chooseContact({ contacts: contacts });
    }
  }

Problem solved. Before sending the contact object to my component that renders it with FlatList, I parsed it into a simpler object with just the fields I needed (because, as per here, using the API fields parameter does nothing, and getContactsAsync always returns everything).

Now the FlatList is rendered very fast in its entirety. I updated my snack to show the new code.

Not sure how to mark a topic as closed, but this one is.

1 Like

Awesome. Glad you figured it out and thanks for sharing the answer with us! I’ll close the topic.

All the best,

Adam