简体   繁体   English

将多个值存储为SharedPreference是一个好主意吗?

[英]Is it a good idea to store multiple values as SharedPreference?

In my android app I have about 100 places (maximum will be 200). 在我的Android应用程序中,我有大约100个位置(最多将是200)。 I want to allow the user to mark each place as visited and store this selection. 我想允许用户将每个地点标记为已访问并存储此选择。

So the user can mark/unmark that he already visited some places/cities. 因此,用户可以标记/取消标记他已经访问过一些地方/城市。

Is it a good idea if I store the values as SharedPreference? 如果我将值存储为SharedPreference,这是一个好主意吗?

My code: 我的代码:

SharedPreferences sharedPref = getActivity().getPreferences(Context.MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPref.edit();
editor.putString("London", "1");
editor.commit(); 

and next time when user marks another place then: 并且下次当用户标记另一个地方时:

editor.putString("Paris", "1");

I am asking due to amount of possible places to be stored there, which will be maximum 200 in my case. 我问的是由于存放在那里的可能位置的数量,在我的情况下最多200个。 I am usually using this kind of storage just to store some settings or so, but I think this is an easy way to store the values in my case too, I don't want to use database or anything similar for storage. 我通常只是为了存储一些设置而使用这种存储,但我认为这也是一种在我的情况下存储值的简单方法,我不想使用数据库或任何类似的存储。

Whether this is a good idea or not depends on your requirements and expectations. 这是否是一个好主意取决于您的要求和期望。 If you store the data like this, it will work for sure, but, there will be some limitations: 如果您存储这样的数据,它肯定会起作用,但是会有一些限制:

  • It might be complicated to show a list of places to the user. 向用户显示地点列表可能很复杂。 If you store some other data to shared preferences you will need a way to distinguish places from other data. 如果将其他一些数据存储到共享首选项,则需要一种方法来区分其他数据。 In this case you'll probably need to add a prefix to all your keys, such as "place_London", "place_Paris", etc. 在这种情况下,您可能需要为所有密钥添加前缀,例如“place_London”,“place_Paris”等。
  • You are relying on English key names so you might have issues with localization if you support other languages 您依赖于英语密钥名称,因此如果您支持其他语言,则可能会遇到本地化问题
  • It will be much harder to support versioning and scalability. 支持版本控制和可伸缩性将更加困难。 Eg if later you have an entity called "Place" and it has more information than just a name with a flag, then it will be much harder to keep it in shared preferences. 例如,如果稍后您有一个名为“Place”的实体,并且它拥有的信息不仅仅是带有标志的名称,那么将它保存在共享首选项中会更加困难。 Eg if at some point you want to add a corresponding country name to all places, what do you do? 例如,如果您想在某个时刻为所有地方添加相应的国家/地区名称,您会怎么做?

I think in this scenario you actually DO want to use database. 我认为,在这种情况下你实际上要使用的数据库。 It will pay off. 它会得到回报。

SharedPreferences is a key/value way to save data. SharedPreferences是保存数据的关键/价值方式。 I think it is not appropriate to save large amount of structured data as you have to define a key for each value you have. 我认为保存大量结构化数据是不合适的,因为您必须为每个值定义一个键。

Using SQLite might be a better option for your case. 对于您的案例,使用SQLite可能是更好的选择。

You should switch to a more reliable way of storing data in storage instead of using SharedPreferences. 您应该切换到更可靠的方式将数据存储在存储中而不是使用SharedPreferences。

Sqlite is good option but if you don't like to write SQL Queries and want a solution where you want to store data in your storage, Realm is an amazing alternative. Sqlite是不错的选择,但是如果你不喜欢编写SQL Queries并想要一个你想在存储中存储数据的解决方案,那么Realm是一个了不起的选择。

Although before implementing please read more on the pros and cons of using realm. 虽然在实施之前请阅读更多关于使用领域的利弊。 You can read more about Realm here :- 你可以在这里阅读更多关于Realm的信息: -

realm.io realm.io

Realm vs Room vs ObjectBox Realm vs Room vs ObjectBox

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM