[英]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:
如果您存储这样的数据,它肯定会起作用,但是会有一些限制:
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的信息: -
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.