Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Recommendations for persisting data on Android?

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.

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.

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.

like image 943
Jeremy Logan Avatar asked Jan 07 '10 21:01

Jeremy Logan


People also ask

What are the various techniques to persist data in Android?

For Android, there are primarily three basic ways of persisting data: A lightweight mechanism known as shared preferences to save small chunks of data. Traditional file systems. A relational database management system through the support of SQLite databases.

Why should you care about data persistence?

With persistent data, there is reasonable confidence that changes will not be lostand the data will be available later. Depending on the requirements, in-cloud or in-memory systems can qualify. We care most about the "data" part.


2 Answers

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.

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.

(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.)

like image 60
Dan Lew Avatar answered Oct 19 '22 12:10

Dan Lew


Also unless your data is at least 100s of Kb, I would suggest just storing it in your private data storage instead of on the SD card. Keep in mind that you can't rely on the SD card being available.

like image 43
hackbod Avatar answered Oct 19 '22 14:10

hackbod