DatumPlane
DatumPlane

Reputation: 321

Shared Preferences vs SQLite for caching JSON

What is the best way to persist a JSON in between Android application life cycles? Right now I am using a rest service to return a JSON. I use this JSON as a data provider for a javascript chart I am loading into a large amount of webviews. I don't want to continuously hitting the server to request the JSON, instead I want to cache it and retrieve it when needed allowing the user to manually update/refresh the data.

Should I be doing this with Shared Preferences or should I persist to a SQLite table? My main concern is still being able to retrieve the stored data even after the application has been killed/restarted. I also have a large amount of JSONs and want to know which would be the most efficient.

Upvotes: 2

Views: 318

Answers (2)

ch3tanz
ch3tanz

Reputation: 3070

If you want to make things easy then you should save JSON Response string into SharedPrefence associating Key to JSON.

Its easy to retrieve to from SharedPrefence. You can simply check that particular KEY is present or not in ShredPrefence, if not then you can hit the server otherwise get JSON Response from ShredPrefence.

When the user manually updates then update the JSON Response in the SHaredPrefence.

Upvotes: 0

Sanjeev
Sanjeev

Reputation: 4375

It depends How Big is you data

SharedPrefences

Store private primitive data in key-value pairs.

If you just want to store 2-3 variables and integer,boolean sharedPreferences will do the trick.

Sqlite

Store structured data in a private database.

If your data contains of lots of item like you are filling a ListView its better to implement SQLite as data is stored in more organize order , you can set id to these data (for example you can set id as today's date and check if current date is greater then the id then you might need to refresh) along with it there are some powerful classes to help you like CursorAdapter and Cursor

Upvotes: 4

Related Questions