xamarin / Essentials

Xamarin.Essentials is no longer supported. Migrate your apps to .NET MAUI, which includes Maui.Essentials.
https://aka.ms/xamarin-upgrade
Other
1.52k stars 505 forks source link

[Bug] Contacts -> GetAllAsync very poor performance on Android #2004

Open cjrvdev opened 2 years ago

cjrvdev commented 2 years ago

Description

When trying to retrieve the full contact list in devices with over 1000 contacts the performance is not appropiate and we observe the ANR graphs in google play console skyrocket to 3-4%. The implementation is not the recommended it seems and many stackoverflow posts talk about this topic. Also found a blog talking about "the good, the bad and the ugly" in this topic which can maybe work to improve the Essentials Library

Steps to Reproduce

  1. Get a device or emulator with many contacts (preferably more than 1000 although more makes it more noticeable)
  2. Call GetAllAsync on Android on any View

Expected Behavior

Contacts load taking its time but not freeze the app.

Actual Behavior

Android show "Application XXX is not responding" prompt as if the main thread is locked.

Basic Information

PS: Thank you very much for all the work put into this library, its awesome :)

pictos commented 2 years ago

When I implemented it I was thinking about it... If the user has a lot of contacts we will have perf. issues... I can see two solutions for this:

  1. Use IAsyncEnumerable;
  2. Create some kind of pagination. If I'm not wrong we can pause the Task execution.
cjrvdev commented 2 years ago

This is the blogpost I read a few weeks ago about it: https://medium.com/swlh/the-good-the-bad-and-the-ugly-three-approaches-to-loading-contacts-in-your-android-application-c96eaf03ffaf

Im not sure if chaning the implementation is something viable but the post has some interesting approaches. Seems like not so long ago Google added Loader classes to deal with this kind of task.

Im sorry I cannot be more helpful about it

pictos commented 2 years ago

@cjrvdev thanks for the reference... Yeah, I don't know if the team wants to make the change here, since this repo is in code freeze mode. If the fix for this issue is something "simple" it can be merged here, but if we have to change the implementation I believe that we should it on MAUI

jfversluis commented 2 years ago

As long as we don't change/add/remove public APIs we're still open for bugfixes! Of course a contribution if this impacts you is much appreciated!