对于坚持在Android上的数据的建议?建议、数据、Android

2023-09-05 10:47:55 作者:等我王者归来

有一个Web服务,提供了一些数据,我的应用程序利用了。这个数据是相当大的,只有变化非常频繁,所以我认为这将是很好,如果该应用程序可以缓存它的SD卡,仅根据需要进行。更新

There is a web service that provides some data that my app makes use of. This data is fairly large and only changes VERY infrequently so I thought it would be nice if the app could cache it on the SD Card and only update it as needed.

目前,我抓住了数据(XML文件),并使用SAX解析成一个对象树。这个过程需要(最多)2-3秒了我的WIFI。然而,序列化所产生的对象到SD卡显著花费更长的时间(一分钟或更多)和反序列化仍需要较长的时间不仅仅是下载/解析摆在首位。

Currently I'm grabbing the data (an XML file) and parsing it into an object tree using SAX. This process takes (at most) 2-3 seconds over my WIFI. However, serializing the resulting objects to the SDCard takes significantly longer (a minute or more) and deserializing it still takes longer than just download/parsing in the first place.

有没有人有此改善或替代的想法坚持这个数据(而不是只保存XML文件,并重新分析每一次)的任何建议?

Does anyone have any recommendations for improving this or alternate ideas for persisting this data (other than just saving the XML file and reparsing every time)?

更新:这不仅仅是一个记录琐碎的集合。对象的图形实际上是可笑复杂并存储到数据库将导致几十个表,在每一个唯一的一条记录。

UPDATE: This is more than a trivial collection of records. The object-graph is actually ridiculously complex and storing it into a database would result in dozens of tables with only a single record in each one.

推荐答案

Android的序列化是出了名的缓慢。我强烈建议切换到使用XML或JSON(或其他),并撰写出该文件的方式。既然你已经有了一个XML解析器,它可能使最有意义只是缓存您下载了原始的XML文件,并根据需要重新分析它。

Android serialization is notoriously slow. I highly suggest switching to using XML or JSON (or whatever) and writing the file out that way. Since you've already got an XML parser, it may make the most sense just to cache the original XML file you downloaded and reparse it as necessary.

我从Serializable接口切换到JSON文件存储在一个应用程序之前和速度的提升是惊人的,幅度至少一个数量级。

I have switched from Serializable to JSON file storage in an app before and the speed increase was incredible, at least one order of magnitude.

(我可能会误解你的问题 - 我假设你使用的是序列化写入光盘如果您再现XML,那么我不知道为什么如此多的SD卡在较慢的同时,我。同意SQLite数据库是很有道理的典型,但正如你已经指出它不适合你的应用程序的需要。)

(I may be misunderstanding your question - I assume you are using Serializable for writing to the disc. If you are reproducing the XML, then I'm not sure why it is so much slower on the SD card. Also, I agree that the SQLite database makes the most sense typically, but as you've already stated it does not fit the needs of your application.)