How many Shared Preferences is too many? - android

I am writing a game and storing almost all active game data (stats, location etc...) in shared preferences.
This will, when all is said and done result in over 100 shared preferences used by my game. Of course the majority of those stored values are small integers or Boolean.
Since I don't need to do any sorting on the stored data, I don't really see a need to use a database.... unless there is some distinct advantage that I am not aware of.
Is there any reason why Shared Preferences should NOT be used in this way? Performance issues? Data Integrity issues? Anything?
Thanks in advance!

If the values remain small, and you don't need them to be structured (like if you have user profiles or something), then Shared Preferences should be just fine. 100 ints only amounts to 400 bytes, so even if the Shared Preferences were stored in memory, it's not a big deal.

There is no limit on the number of shared preferences (except for storage space), but these are currently written as a single XML file for the entire shared preference object, so you
don't want to go crazy with how much you put there. 100 preferences shouldnt be an issue, and it allows a quicker and simpler access to data compared to databases or flat files etc.

It is actually an xml file. In your case, there is certainly no problem for your concerns.

Related

Is it okay to use shared preferences for big data objects?

I know there are some topics and posts about this question, but i want to have an individual feedback.
So in my app i use one shared preferences (file) and gson to store a list of objects, containing big amount of data. So my shared preference file is about 5 MB big and contains ca 1 Mio characters. I know this sounds not really good, but my app works perfectly. No long loading or saving times.
I choose this method, because i ONLY want to store the data, when my app will be closed and between app starts.
I know really good, that shared prefs is not a good way and actually meant for saving small strings for preferences.
So do i have to change this method or is it "okay"? Or does someone know a good solution, which is easy to use and understand, because for me sqlite is difficult and hard to use. Or is there a easy way to store an object into sql? (Maybe a library or a method??)
Thank you in advanced!
Rule of thumb: As long as it works does not necessarily mean it's good. Said that, SQLite is a perfectly valid solution for android applications. There is also a handy developer guide on how to save data with SQLite, but the recommended approach is actually to use the Room Persistence Library.
No, it is not meant for big data objects. For that for example you can use SQLite internal database
for temporary bases it is good for store a data in shared preference. If you want to store a big data then you have to use SqLite to store your data in your device.

Max number of shared preference files?

I want to store some data in shared preferences files instead of sqlite for the sake of simplicity and to avoid the overhead and boilerplate of sqlite.
Is there any limit to the number (not size) of SharedPreferences files a single application can create ?
Is it safe to assume that less than a hundred files would be fine ?
All shared prefs are stored in /data/data/[package
name]/shared_prefs/[app name].xml, so i think there's no limit based
on aechitecture.
I don't know practical limit but i think it's enough for you :D.
There is no limition for sharedPrefrence file. You can save multiple file but it bad habbit. Sqlite is best option for that otherwise you can serialize and then save to it shared prefrences.
there is no limit in Shared Preference.
its stored in /data/data/[package name]/shared_prefs/[name].xml
you can see this file if your phone is rooted
You can also use SQlite Db ,if you need to store more data use DB,because its easy to handle data with databases
Its depend on the free memory on device. But don't use SP for storing huge amount of data. It may crash your application sometimes.

Shared Preferences "limit"

I know similar question to this one has been asked a numerous times, and surfing through SO I partially found an answer, but not complete, and android docs don't really help. Obviously I know how they work and have used shared preferences many times before, but I am wondering at what point ( how many ) is too much, I've read people had ~ 100KBS stored without any problem. Long story short --
Did someone actually had problems with too many data stored in shared preferences and what was problem, does data get deleted or?
** this is only a question out of curiosity, I already have my large values stored in SQL DB, just wondered what would be and if there would be any problems if someone for some reason stored everything in shared preferences
Since SharedPreferences are stored in an XML file, and therefore lacks the strong transaction support of SQLite, I would not recommend storing "100KBS" in SharedPreferences.
That being said, the lowest size limit that I am aware of will be your amount of free heap space, as SharedPreferences reads that entire XML file's contents into memory.
There is a limitations of SharedPreference data. In my case it throw a Memory Exception when SharedPreference data cross 1428.51-kb.
So its better to use SQLite database when you required huge data to store.
From reading your question I would think that you should not be using SharedPreferences, because (a) they are intended for storing much smaller amounts of data (hence the use of XML), and (b) there are many simple alternatives.
The only thing 'special' about SharedPreferences is the integration with the Preferences Activity for showing your preferences to the user, and that is probably not applicable in your case based on the amount you plan to store. (Oh, also the SharePreferences handles concurrency issues for you.)
You could use Java's serialization to store Preference class(s) in binary files. These would be dramatically smaller then comparable PreferenceFile and can easily be passed through GZIPInputStream to make it smaller (or CipherInputStream) to encrypt it. I have found this alternate to be a powerful, simple, and cross-platform way to store app data where the power of SQLite is not needed.
(Sorry this isn't a direct answer.)

Disadvantages of using SharedPreferences?

What are the disadvantages of using too much SharedPreferences on android?
If you try to do anything complicated with SharedPreferences quickly becomes very tedious; you can only store primitive types and it's a simple key/value system. So, there are no disadvantages per se, but SharedPreferences are meant for lightweight storage of simple data.
You should only use SharedPreferences to store small bits of data related to user configuration. It can only store basic data types, so if you have more complex bits of information, you should probably switch to another mechanism.
One convenient feature of SharedPreferences is that you can share data between two applications using it; so if you have two applications that have to share user preferences with each other, using SharedPreferences might be a convenient choice.
Also, you should not store large amounts of data in SharedPreferences; it's not made for that. Instead, use an SQLite database(1) for a more robust solution.
(1) http://developer.android.com/reference/android/database/sqlite/package-summary.html
Shared preference are for quick access and are loaded in the memory for fast access along with the application...so if you try to store large amount of data in shared preference...it is going to cause some more usage of critical memory of your app(and i guess if too big to store in memory some swapping would happen causing performance impacts).Better to use DB/file system/web server for huge data....by storing small quantities of information that are very often accessed by the app you can in-fact increase your application performance.
user can clear data which is stored in Shared preference at any time. this is the major drawback of Shared preference.
first point to be considered is shared preferences are non-volatile data. So they are stored as key-value pairs as your application data. So, using them extensively in your app might become complicated for you. And, coming to process throughput, it don't effect your app speed because these are stored in/as secondary storage.

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

Categories

Resources