Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is using Android shared preferences for storing large amounts of data a good idea?

So I inherited this Android project from someone else. The code currently seems to be storing huge amounts of data (that should really belong to an SQLite database) into the shared preferences. I'm very uncomfortable with that part of the code and want to start using the sqlite database. But I am still unable to justify to myself the time it would take especially if it comes with no immediate benefits.

Of course I'm eventually going to move it to sqlite but since I'm kinda on a tight deadline I was wondering if this is worth something doing now or later.

Any thoughts and comments on storing large amounts of data in shared preferences would be very much appreciated.

Thanks

like image 636
chhabrakadabra Avatar asked Jun 13 '11 02:06

chhabrakadabra


2 Answers

If it works now then you can definitely leave it. You are correct that the large amounts of data should go into the database. If nothing else, you'll have an easier time of querying for data.

Further research has found this post suggesting that you won't have any major problems with a large amount of data in your Shared Prefs. You could, however, have performance issues since the single Shared Pref XML file will have to be read to get any pref while with a database you only have to grab what you need as you need it.

like image 67
Haphazard Avatar answered Oct 01 '22 06:10

Haphazard


TL;DR; Don't use shared prefs for large storage, use a DB instead (but if it works for now and you're in a rush do it later)

I wouldn't personally recommend it since the system will keep an in-memory copy of all shared prefs for your app. So if you throw a lot of data in there your memory usage will be affected. That said, and depending on the data format (whether you can use it as is and use the key to find it directly - if you just store a huge JSON object that you then have to parse or if you have to get all shared prefs to then do a linear search for the one you really need, there's little benefit in either case) and how often you have to access it, it might be faster to lookup than a file or a database since it's already in memory. It also provides the benefit of being threadsafe (a SQL DB would be as well since DBs get locked when accessed) as opposed to a file where you would have to deal with that on your own.

Hope this helps.

like image 31
Francois Dermu Avatar answered Oct 01 '22 06:10

Francois Dermu