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

后端 未结 2 1818
孤街浪徒
孤街浪徒 2021-02-07 22:31

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 sha

相关标签:
2条回答
  • 2021-02-07 22:54

    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.

    0 讨论(0)
  • 2021-02-07 23:16

    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.

    0 讨论(0)
提交回复
热议问题