问题描述
我的一些应用程序的用户的抱怨有时(以随机的方式),我的应用程序的设置将得到恢复到默认状态(通常是手机重新启动后)。我从来没有设法虽然重现该问题。我想这是由于这样的事实,在很多地方在我的应用程序我有一张code调用的共享preferences编辑和提交的变化 - 它能否解决在破坏共享preference文件如果我尝试提交一些变化到相同的preference文件在同一时间? (多线程应用程序)
Some of the users of my application complain that sometimes (in a random way) the settings of my application are getting reverted to their default state (usually after a reboot of the phone). I never managed to reproduce the problem though. I am thinking it is due to the fact that in many places in my app I have a piece of code that calls the shared preferences editor and commits changes - Can it resolves in corrupting the shared preference file if I try to commit several changes to the same preference file at the same time? (Multi-thread application)
我是真的失去了。我试着在网上找了几个小时找到没有一个成功的解决方案。
I am really lost. I tried to look in the web for hours to find a solution without a success.
如果任何人有更是一种理念,所以我就可以开始调查,我将不胜感激。
If anyone has even an idea so I can start investigating, I would be grateful.
谢谢,阿米特·莫兰
推荐答案
我会附和其他的答案 - 你需要避免冲突,如果你不想破坏的文件 - 和我会走得更远建议那你可能misuing共享preferences。
I'd echo the other answers - that you need to avoid conflicts if you don't want to corrupt the file - and I'd go further to suggest that you're probably misuing SharedPreferences.
的SP被设计用于存储小件有关您的应用程序 - 用户的设置,如音量,还是音乐播放或类似的事情
SPs are designed to store small pieces of information about your app - user settings like volume or whether music is playing or things like that.
的SP没有设计用于存储数据往往和/或大量数据的变化,这是一个坏主意,试图做到这一点(为你所发现的原因和其他一些)。
SPs are NOT designed for storing data which changes often and/or large amounts of data and it's a bad idea to try to do this (for the reasons you've discovered and a few others).
记住的SP其实只是一个XML文件 - !你招致解析的开销和重建,每一次你改变它太
Remember that SPs are really just an XML file - you're incurring the overhead of parsing and recreating that every time you change it too!
一个应用程序更新的SP在多个线程的想法是有点疯狂,我认为 - 在一个以上,将支付过你 - 你需要管理和存储你保存数据的更好的方法这样...
The idea of an App which updates SPs in more than one thread is a bit mad I think - you need a better way of managing and storing the data you're saving - it will pay-off for you in more than one way...
这篇关于Android的 - 共享preferences有时会丢失的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!