Loading All Contacts

Hello,

Trying to get all the contacts on my phone takes a brutally long time.

I have logged it using the bottom code:

_GetAll(total) {
		var now = new Date();
		Expo.Contacts.getContactsAsync({
			fields: [
				Expo.Contacts.PHONE_NUMBERS,
				Expo.Contacts.EMAILS,
			],
			pageSize: total,
			pageOffset: 0,
		}).then(({data, hasNextPage, hasPreviousPage, total}) => {
			this.setState({
				contacts: data,
				count: total
			});
			console.log((Date.now() - now.getTime()) / 1000, 's');
		})
	}

total is the total value returned from the API. In my case, I am trying to get 776 contacts. It takes on average 40 seconds to load all contact.

Please advise if there is a better way of doing this.

Thank you.

I believe this is why we offer a paging API for it. Especially on Android, this API is slow. We use a standard native implementation to fetch these, we aren’t doing anything special or different which would add extra slowness.

1 Like

Thanks for the quick response ben. Will look into a way to optimize the code.

Ultimately looking for a way to load all the contacts so that I can filter them by a text search.

The android api is just slow on some phones which is why we offer paging. Our source code is available at GitHub - expo/expo: An open-source platform for making universal native apps with React. Expo runs on Android, iOS, and the web. but I think it’s just a slow api.

I think what we want here is a way to query the contacts without having to load them all into memory. I created a feature request for this here: Query contacts | Voters | Expo

2 Likes

Currently I’m using a FlatList to display contacts. It is slow, because my users want a sorted list (don’t blame them). This requires retrieving all of them in one go, because the current API does not sort them.

This slowness basically makes the API useless to me, I am now resorting to creating my own contact list, and trying to keep it in sync in the background, which is quite an ordeal frankly.

How do other people solve this?

1 Like

Ran into the same issue last days. The paging API is only useful if it returns sorted list values. Otherwise people always need to fetch the whole thing, sort them and show them or make another pagination. Haven’t found a proper solution for this yet.

We’ll need to make native changes to sort the result. Would welcome a PR!

1 Like

I dont understand native code well enough, but I think this stackoverflow solution shows that there may be a way to make this quicker on Android. I am also pulling in all the users contacts in my app at once. On iOS it is quick but on Android it runs slowly.

Here is the solution I found:

For my app only the email, phone number and name is necessary too. Maybe an option to select only what is needed could make this faster?

@ezinator - you can actually do that already, specify the fields option when calling getContactsAsync: https://docs.expo.io/versions/latest/sdk/contacts.html#fields-array--an-array-describing-fields-to-retrieve-per-contact-each-element-must-be-one-of-constants-listed-in-the-table-below

@notbrent Yes I am using the fields option, but when I log the resulting object, even though I am only asking for the email and phone number I get a very large object with other default keys. The fields option seems to be tacking on addons to a large default object

I was wondering if instead we could just call the fields we want and we would only get that, if that would make the api call faster

I suspect a lot of us actually just want to get the user to select one contact.

So, if Expo exposed contact picker API then, for those users, it would eliminate the problem being discussed here, eliminate the need for all of our contact management code (and saving the contacts), and reduce related privacy concerns.

And I see someone offering to add this:
https://forums.expo.dev/t/how-to-contribute-to-the-expo-sdk-add-native-code/4458

I’m sure it is more complicated then I realize, but I hope Expo will consider this.

Thanks.

1 Like

I love using Expo but this really slow read of android contacts is a showstopper for us. Is there any chance that someone will work on this problem soon?I’d love to contribute by hiring someone to work on this, what specific skills should I be looking for?

fyi for those still following here, android optimization is in flight: https://github.com/expo/expo/pull/1349.

disclaimer: not part of expo team and above is not meant to be a confirmative statement, expecting continued good collaboration with expo for quick integration of the optimizations :slight_smile:

2 Likes

Hi there is news about the fix? because in expo 26 the situation is the same . . . is so slow in adnroid…

Hey @izzusan,

The PR mentioned above is set to be merged and should be released with SDK27 so when we release that, there will be performance improvements to the Android Contacts API. We don’t have a date or estimate of when that will be so follow our blog on Medium or follow us on Twitter so you know when we roll out SDK27.

Cheers,

Adam

2 Likes

Just wanted to add another friendly voice to the chorus of folks who have the same challenges as (username)izzusan and (username)heresmyinfo (not allowed to tag many people people bc I’m new) regarding android-contacts loading. We will hold our app’s release due to this bug and are excitedly awaiting the release of 27.

Also wanted to say special thanks to @tiny-dancer for working this issue as it’s so critical for us right now, and we are happy to have found it will be resolved in this next release.

Again, thanks to all of you for all you do :grin: :pray: :bowing_man:

2 Likes

this is improved in sdk27

Is there still no way to do simple pagination or even search for one single contact without pulling the entire list of all contacts?