将多个类的数据存储在中心对象中
我有一个应用程序严重依赖来自 Web 服务的 JSON 响应。应用程序的所有部分都包含根据这些响应构建的对象,这意味着这些数据对于应用程序的正常运行非常重要。
目前,每次用户打开应用程序的功能(例如新闻、音乐、图片等)时,都会查询 Web 服务并以所需的数据进行响应。然后,该数据被解析并用于向用户显示他们选择的内容。 问题是,这个过程对带宽非常不友好。特别是对于连接速度较慢的手机,加载和解析数据可能需要几秒钟的时间,更不用说下载将要显示的图片了。 如果用户关闭该功能,然后再次打开它,所有数据都会重新下载并重新解析。
因此,我想将最常用的数据保存在一个中心类/对象中。由于这些数据在用户使用应用程序时不太可能更改,因此应该可以将所需的数据保存在某处,以防用户重新打开他们刚刚关闭的功能。
但我该怎么做呢?我读到了有关 Singleton 等的内容,但是如何将来自 20 多个不同类的数据添加到 Singleton 对象中呢?当用户重新打开函数时,我将如何检索这些数据并将其添加到新的意图中(如果这甚至可能的话)
I have an app that relies heavily on JSON responses from a webservice. All parts of the app contain objects build from these responses meaning this data is very important for the app to run properly.
Currently, every time a function of the app gets opened by the user (for example news, music, pictures, et cetera), the webservice gets queried and responds with the desired data. This data then gets parsed and used to show the user whatever it is they chose.
The problem is, this process is very bandwidth unfriendly. Especially for phones with slower connections it might take a few seconds to load and parse the data, let alone download the pictures that will be shown.
If a users closes the function, and then opens it again, all data gets re-downloaded and re-parsed.
Therefore, I want to save the most commonly used data in a central class / object. Since this data is unlikely to change while the user is using the app, it should be possible to save the required data somewhere in case the user reopens a function they just closed.
But how would I do such a thing? I read about Singletons and such, but how would I add data to a Singleton object from over 20 different classes? And how would I retrieve this data and add it to a new Intent for when the user re-opens a function (if this is even possible at all)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
一个建议,但不一定是最好的方法,我不确定我是否正确解释了你的问题。
您可以将其中任何一个存储在 sql lite 数据库中或者 SD 卡上吗?
对于音乐和图像,您可以将 SD 卡上项目的路径存储在数据库中。
然后,每次数据更改时更新数据库,无论是每次应用程序加载时还是通过调用“数据是否已更改”链接?
A suggestion but not necessarily the best way, I am not sure if I have interpreted your question correctly.
Would you be able to store any of it in an sql lite db or maybe on the sd card?
For the music and images you could store the path to the item on the sd card in the DB.
Then update the db each time the data changes either each time the app loads or by calling an "has the data changed" link?
我在高分游戏中遇到类似的问题 - 高分列表很大,并且来自 HTTP Get 请求的 JSON 形式。我用:
- 单例高分服务,负责缓存本地 JSON 副本,处理来自 HTTP 的检索等。由于加载高分需要时间,因此它还会执行异步处理,并在新的高分可用时触发广播(游戏活动在需要时会接收它们)
- 用于定时广播的广播接收器,以执行定期的 higscore 同步
我使用自己的数据绑定库进行 JSON 数据存储:
https://github .com/ko5tik/jsonserializer
I face similar problem in my games with highscores - highscore lists are big and come as JSON from HTTP Get requests. I use:
- singleton highscore service which is responsible for caching local JSON copy, handles retrieval from HTTP etc. Since loadfing higscores taks time, it also performs asynchronous processing and fires broadcasts when new highscores are available ( and game activities receive them if they need it)
- broadcast receivers for timed broadacasts to perform regular higscore synchtronisation
I use my own databinding library for JSON data storage:
https://github.com/ko5tik/jsonserializer