1

First of all, don't judge me.

I'm storing a JSON in my SharedPreferences and it has around 200KB. It is a JSON with more than 30 custom and big objects, and every time the user logs in I need this data (I know it sounds like poor design, but that's my current situation). Instead of downloading it every time, I saved it to my Preferences.

From what I've read here and here, I didn't think I'd have problems. However, I tried loging in and out a bunch of times and instead of seeing my 30 objects, I got only 16. When I log the size of this JSON, it returns me 0 (because it's incomplete and it doesn't have a EOF character?).

I tried saving its value to a file but it just didn't work (and apparently no exceptions were thrown) when creating the file.

I wanted to be sure this is the problem before moving to another solution like storing into a db, but I just didn't find information about that.

So, my question is, are Preferences "reliable" in the matter that its data is safe and the xml file won't get corrupted?

EDIT: I didn't make it very clear. I am converting my JSON to String and storing it as a value in my Preferences, and when I retrieve it I convert it back to JSON.

I misinterpreted Darren's answer and came with the idea of just saving this JSON in a separate file, apart from Preferences (harder to get corrupted?). I'll try it later and post the result.

Community
  • 1
  • 1
Teo Inke
  • 5,928
  • 4
  • 38
  • 37
  • I'm storing this JSON as a String. After retrieving it I convert it back to JSON. – Teo Inke Jul 17 '15 at 18:48
  • 2
    Have you considered storing the raw JSON data in a file for itself? SharedPreferences is not designed for storing this type of data, so you might want to consider storing it in a file on the internal storage. [Using the Internal Storage](http://developer.android.com/guide/topics/data/data-storage.html#filesInternal) – Nicklas Jensen Jul 17 '15 at 19:02
  • That's the idea I came up from Darren's post. I'll try it later, it should be safer – Teo Inke Jul 17 '15 at 19:06
  • Why you even bother? If data from sp or file could not be parsed then are corrupted... So you should download email again... Seriously, you shouldn't worry about this... I would use a file because before you getting the json from sp system need to parse the XML... And again, if you are saving it it need to be converted to XML(escape special chars, etc.etc.) – Selvin Jul 17 '15 at 19:08
  • I don't really understand why you don't just us a DB - it's far more versatile. – Squonk Jul 17 '15 at 19:23
  • I only have one JSON, one String to save. Why use a DB for that? – Teo Inke Jul 17 '15 at 19:26

2 Answers2

0

I don't think shared preferences data can be corrupted, but it can be changed(hacked) by rooting the device and accessing /data/data/YOUR_PACKAGE_NAME/shared_prefs/ so its advised to encrypt it when using shared preferences.

Here's a solution for encrypting shared preferences. https://github.com/scottyab/secure-preferences

Hope it helps

Jitin Jassal
  • 137
  • 2
  • 13
-1

SharedPreferences should not get corrupted, unless the memory on which it is stored is faulty.

guido
  • 184
  • 4
  • Would you have any references to support your statement? That's what I've read before, but nothing official. – Teo Inke Jul 17 '15 at 18:55
  • The documentation states you can use it to save preferences, if there was any chance in which you could lose this data it would be stated in the API documentation. I never ran into problems when using SharedPreferences for saving my settings. You could only get a problem when you save a value as a String and try to load it as a Boolean (for example in a newer version of your app). If you save a huge amount of data, it might cause some memory problems, but in that case I would expect an error in your logs. – guido Jul 17 '15 at 19:23