重新启动活动时快速加载数据

发布于 2024-10-02 05:45:04 字数 513 浏览 2 评论 0 原文

实际上,我从网络上获取 XML,解析它并将其显示在屏幕上。我遇到的问题是,每次调用该活动时,该活动都会尝试从网络加载数据。(实际上,我的移动应用程序需要频繁访问服务器)所以我正在寻找以下解决方案:

On Re-launch of an activity, the data from web should not be loaded again. 
creating a class that i can use to transparently make application faster by
selectively caching items in memory.  And thus, making users very happy and 
This class can even apply individual refresh times to each cached item.

所以,我我认为我需要将数据存储在缓存或类似的东西中。

有什么方法/技术来实现这样的事情吗?这样就不需要每次重新启动活动时都加载。

Actually, i am fetching XML from the web, parsing it and display it on the screen. The problem which i am getting is that the activity tries to load the data from web each time the activity is called.(Actually, my mobile app requires frequent trips to the server) So i am in search of the below solution:

On Re-launch of an activity, the data from web should not be loaded again. 
creating a class that i can use to transparently make application faster by
selectively caching items in memory.  And thus, making users very happy and 
This class can even apply individual refresh times to each cached item.

So, i think i need to store the data in Cache or something like that.

Is there any way/technique to implement such thing ? so that it need not to load every-time on Re-launch of an activity.

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

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

发布评论

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

评论(3

烟燃烟灭 2024-10-09 05:45:04

不确定此建议的性能如何,但如果您只是想避免每次活动重新启动时都获取 XML 文件,那么到目前为止最简单的方法是将它们保存到 内部存储。当您的活动(重新)启动时,您将首先检查文件是否在本地存在,然后再从网络获取它们。

当然,您需要某种逻辑来判断 XML 文件是否“过时”(您可以有一个简单的数据库表,其中包含文件名和上次获取的日期),并且您可能还希望有某种当您不再需要文件时删除文件的清理逻辑,这样它们就不会填充不必要的空间。

Not sure what the performance would be with this suggestion, but the by far easiest if you just want to avoid fetching the XML files every time your activity re-launches, is to save them to Internal Storage. When your activity (re-)launches, you'll start by checking if the files exist locally before you fetch them from the web.

You would of course need come kind of logic to tell whether an XML file is "out of date" (you could have a simple database table with file names and last-fetched dates), and you would probably also like to have some kind of clean-up logic to delete files when you no longer need them so they don't fill up unnecessary space.

萧瑟寒风 2024-10-09 05:45:04

您可以尝试使用 Service 在给定的后台加载数据间隔。

You could try using a Service for loading the data in the background at given intervals.

怪我闹别瞎闹 2024-10-09 05:45:04

那么我要做的就是创建一个表,假设将其称为“Table_Web_XML”,并在其中添加适当的字段来存储特定的 Web 数据。

启动活动时,我首先检查数据库中是否有数据。如果表为空(意味着活动是第一次加载),我会从网络加载它并显示在屏幕上并保存在数据库中。

另一方面,如果表中有数据,那么我会将其加载到屏幕上,并在后台启动一项服务,如果 Web 上的数据库表有任何更改,该服务将更新我的数据库表。从系统响应的角度来看,这样做的目的是,当重复打开活动时,数据将始终快速加载,并且后台服务将确保后续请求有新的和最新的内容:)。希望有帮助。

Well what I would have done is to make a table , lets say call it "Table_Web_XML" , and appropriate fields in it to store specific web data .

When starting the Activity I would have first check if there is data in the database. If the table is empty (meaning the Activity is loading for the first time ) , I would load it from the web and display on the screen and save in database.

On the other hand if there is data in the table, then i would load it on the screen, and would start a service in background that would update my database table if there is any changes in it on the Web . What this would do is for system response point of view, the data will always be loaded fast, when the activity is repeatedly opened, and background service will ensure new and latest content on subsequent requests :) . Hope it helps.

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