在 Android 上使用模式单例

发布于 2024-12-02 05:03:12 字数 152 浏览 7 评论 0原文

我正在开发一个从服务器检索一些数据的应用程序。 我有两个选择:

  • 从服务器获取整个数据集,然后使用“单例”模式来减少查询数量

  • 对于每个查询,从服务器获取相应的数据集

最好的选择是什么?

I am developping an application that retrieves some data from a server.
I have two options:

  • Get the whole data set from the server and then work with it using the pattern 'singleton' to reduce the number of queries

  • For each query, get the corresponding data set from the server

What is the best choice?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

孤千羽 2024-12-09 05:03:12

我认为这取决于。

这取决于数据的大小,以及返回用户可能不需要的数据是否有意义。

就我个人而言,在我正在构建的应用程序中,我将仅返回当时所需的数据。但显然,一旦我获得了数据,如果永久保留甚至暂时保留它有意义的话,我就不会再次获取它。

In my opinion it depends.

It depends on the size of the data and if it even makes sense to return data that your user may not even need.

Personally, in my app that I am building I will be returning only the data that is required at that time. Obviously though, once I have the data I won't be fetching it again if it makes sense to keep hold of it for good or even just temporarily.

鹊巢 2024-12-09 05:03:12

我同意C0deAttack的回答。您的目标应该是在您的应用程序成为手机上的“好公民”的限制范围内最大限度地减少网络流量。 (这意味着您的应用程序不会通过使用太多资源(包括内存和文件空间)对用户或其他应用程序产生负面影响。)

从听起来来看,我猜测数据并没有那么庞大。如果是这样,我建议缓存响应并在本地使用它,从而避免重复查询服务器。根据数据更改的频率,您甚至可以考虑将其持久化,以便应用程序下次启动时不必查询服务器。如果响应包含被视为过时之前的估计时间,这将有助于建立更新时间表。 (Google 的许可证服务器使用这个想法。)

PS 我不认为这与单例模式有任何(直接)关系。

I agree with C0deAttack's answer. Your goal should be to minimize network traffic within the constraints of your app being a "good citizen" on the phone. (That means that your app does not negatively impact the user or other applications by using too many resources — including memory and file space.)

From the sound of it, I'm guessing that the data are not that voluminous. If so, I would recommend caching the response and use it locally, thus avoiding repeated queries to the server. Depending on how often the data changes, you might even consider making it persistent, so that the app doesn't have to query the server the next time it starts up. If the response includes an estimated time before it is considered outdated, that would help in establishing an update schedule. (Google's license server uses this idea.)

P.S. I don't see that this has anything (directly) to do with a singleton pattern.

掩于岁月 2024-12-09 05:03:12

将数据存储在 sqlite 数据库中并使用 sql 进行查询怎么样?由于您可以免费进行排序和排序,因此它可以帮助您编写更少的代码。如果您的用户没有互联网连接,您还可以使用即时离线功能。 :)

How about storing your data in an sqlite database and do your queries with sql? Since you get sorting and ordering for free, it can help you writing less code. Also you have instant offline functionality if your user has no internet connection. :)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文