简体   繁体   English

如何有效地从数据库检索数据?

[英]How to retrieve data from a database efficiently?

I'm building an android app, and I need to retrieve a list of users from a database. 我正在构建一个android应用程序,我需要从数据库中检索用户列表。 I know how to write the query. 我知道如何编写查询。 I will have back a list of object. 我将返回一个对象列表。

I was wondering how to do this in an efficient way considering that I need that list in 3 activities. 考虑到我需要3项活动中的清单,我想知道如何以有效的方式进行此操作。 I think that repeating the same code for 3 times, in 3 different activities is not optimal. 我认为在3个不同的活动中重复3次相同的代码并不是最佳选择。

Thanks 谢谢

As per you comment you are fetching this user data from server, you can consider following: 根据您的评论,您正在从服务器获取此用户数据,可以考虑以下事项:

  1. make a web service to retrieve user list. 制作Web服务以检索用户列表。
  2. call the web service first time you need list of users. 首次需要用户列表时,请致电Web服务。
  3. cache this list in shared preferences or SQLite DB, depending upon size of list. 根据列表的大小将此列表缓存在共享首选项或SQLite DB中。
  4. use it where ever you need. 随时随地使用它。
  5. refresh your list from server periodically or as per your need. 定期或根据需要从服务器刷新列表。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM