关于SharedPreferences的一次问题

SharedPreferences可以用来保存一些很简单的数据,对应的就是一个key-value。但是最近遇到一个问题就是在多进程的时候,没有办法同步保存数据。看到一个解决办法是Context.MODE_MULTI_PROCESS 来进行多线程访问。在官网在介绍这个方法的时候是这样子的:

This constant was deprecated in API level 23.
MODE_MULTI_PROCESS does not work reliably in some versions of Android, and furthermore does not provide any mechanism for reconciling concurrent modifications across processes. 
Applications should not attempt to use it. 
Instead, they should use an explicit cross-process data management approach such as ContentProvider.

SharedPreference loading flag: when set, the file on disk will be checked for modification even if the shared preferences instance is already loaded in this process.
This behavior is sometimes desired in cases where the application has multiple processes, all writing to the same SharedPreferences file. 
Generally there are better forms of communication between processes, though.

This was the legacy (but undocumented) behavior in and before Gingerbread (Android 2.3) and this flag is implied when targetting such releases. 
For applications targetting SDK versions greater than Android 2.3, this flag must be explicitly set if desired.

在6.0以上这个方法已经被弃用了,最低支持2.3。而且最好使用ContentProvider
一定要用的话,要尽力避免同时去操作SharedPreferencesde。其实也找到一些开源的库,可是觉得没有必要,所以还是算了。

放一些链接:
- https://github.com/android-cn/android-discuss/issues/135
- http://zmywly8866.github.io/2015/09/09/sharedpreferences-in-multiprocess.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值