使用Android共享首选项来存储大量数据是个好主意吗?

时间:2021-08-31 15:59:24

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.

我从别人那里继承了这个Android项目。目前的代码似乎是将大量的数据(实际上应该属于一个SQLite数据库)存储到共享的首选项中。我对这部分代码感到非常不舒服,希望开始使用sqlite数据库。但我仍然无法向自己证明这需要的时间,特别是如果它没有立即的好处。

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.

当然,我最终会把它搬到sqlite,但是因为我有点太忙了,所以我想知道这是否值得做些什么。

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

任何关于在共享首选项中存储大量数据的想法和评论都将非常受欢迎。

Thanks

谢谢

2 个解决方案

#1


9  

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.

进一步的研究发现,这篇文章表明,在共享Prefs中使用大量数据不会有什么大问题。但是,您可能会遇到性能问题,因为要获取任何Pref,必须读取单个共享的Pref XML文件,而使用数据库,您只需要在需要时获取所需的内容。

#2


0  

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.

我个人不会推荐它,因为系统会为你的应用保存所有共享的prefs的内存拷贝。所以如果你在里面放很多数据,你的内存使用就会受到影响。表示,根据数据格式(您可以使用它作为是否和直接使用找到的关键——如果你只存储一个巨大的JSON对象,然后解析或如果你需要得到所有共享控制台然后做一个线性搜索一个你真正需要的,在这两种情况下小好处),经常需要访问它,这可能是查找速度快于一个文件或数据库,因为它已经在内存中。它还提供了线程安全的好处(SQL DB也可以,因为访问时DBs会被锁定),而不是必须自己处理的文件。

Hope this helps.

希望这个有帮助。

#1


9  

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.

进一步的研究发现,这篇文章表明,在共享Prefs中使用大量数据不会有什么大问题。但是,您可能会遇到性能问题,因为要获取任何Pref,必须读取单个共享的Pref XML文件,而使用数据库,您只需要在需要时获取所需的内容。

#2


0  

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.

我个人不会推荐它,因为系统会为你的应用保存所有共享的prefs的内存拷贝。所以如果你在里面放很多数据,你的内存使用就会受到影响。表示,根据数据格式(您可以使用它作为是否和直接使用找到的关键——如果你只存储一个巨大的JSON对象,然后解析或如果你需要得到所有共享控制台然后做一个线性搜索一个你真正需要的,在这两种情况下小好处),经常需要访问它,这可能是查找速度快于一个文件或数据库,因为它已经在内存中。它还提供了线程安全的好处(SQL DB也可以,因为访问时DBs会被锁定),而不是必须自己处理的文件。

Hope this helps.

希望这个有帮助。