Question

i would like to ask a recommendation about data persistence on android.. the thing is this: I have a small rest app that get a json data from a RESTful API that is de-serialized with jackson into an custom object and after that i use this object to feed my ui, the problem is that i want this data to be persistent or at least cache-able.

I saw there are some options that i could use: ORM-lite to save the object in a sqlite DB or save the JSON String into the shared Preference or a file in the internal memory, i already have read the article on the android developers site about this topic.

The structure of my object has at least 15 fields and it is related to an other object.

Which would be a good option?

Was it helpful?

Solution 2

That really depends on a few other variables.

Shared Preferences are great for iterable key/value pairs, but aren't really designed for relational data. They're also meant to store a finite amount of data, not an arbitrarily large dataset. For instance, you would save a user's name in SharedPref, but probably not a complete history of every score the user achieved in every level, or the definition of every level.

SQLite is much better at relational data, since it's a relational database, but it might be overkill if you only ever want to hold on to one object at a time. Typically you'd need to set up a content provider, but I assume ORMLite would handle that for you.

So: If you're only storing one or two instances of this json object at any given time, probably shared pref just for convenience. If you're storing arbitrarily large number of them, and the data it relates to, SQLite database.

OTHER TIPS

Personally, I would recommend saving the JSON string as a file in the applications data area, at least assuming that it isn't so big that parsing it is slow. Generally, parsing JSON is quite fast.

If you need to store multiple versions of this JSON object then a DB would probably be the way to go, however.

I would not recommend using SharedPreferences in this way.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top