Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
122 views
in Technique[技术] by (71.8m points)

android - Should I store my contacts list in DB or Sharedpreference to avoid running the same task

I have my app such that when entered in a certain activity, I show a list of contacts in my phone( similar to Chat app where you see a list of contacts when trying to start a new chat). So I just wanted to know about the implementation details of these task for best/ efficient work. Is the contact list is fetched everytime from my device whenever I enter the activity(which is actually redundant, because we maybe doing same work over and over again, But again we cannot rely on saved data in DB/SharedPref as contacts data is dynamic and bound to change, so need your expert suggestion how to actually make this work in best possible way(I have already written method to fetch contacts- so whould I fetch the contacts afresh everytime or any other way around is there, what the popular chat apps implemet to show the contacts everytime?

question from:https://stackoverflow.com/questions/65903895/should-i-store-my-contacts-list-in-db-or-sharedpreference-to-avoid-running-the-s

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Personally, I would implement the fetching of the contacts on a background thread (using coroutines) that is called periodically (perhaps each time they the activity is created, as you suggested, or maybe once when they open the app).

Because it's on a background thread, it would prevent them using the app (or any dropped frames while the activity is created). They might have thousands of contacts or be using a really slow device.

Presumably, you would want to store some other data associated with each contact: for example, if you opened up a chat with the user, you would want to associate all the messages with the user, so I would use a database such a realm rather than shared preferences.

So the entire process would probably look something like this:

  • Opening the app triggers a service that fetches all of the user's contacts on a background thread.
  • Service updates the database, perhaps removing contacts which don't exist and adding the new ones.
  • If you were handling this in a view model, your fragment or activity could observe the list of contacts and update the UI once it had changed.

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...