SharedPreference在使用过程中有什么注意点?

1745次阅读  |  发布于5年以前

SharedPreference在使用过程中有什么注意点?

commit()和apply()的区别

返回值

apply()没有返回值,而commit()返回boolean表明修改是否提交成功。

操作效率

apply()是将修改数据原子提交到内存, 而后异步真正提交到硬件磁盘,。

而commit()是同步的提交到硬件磁盘。

因此,在多并发commit()的时候,会等待正在处理的commit保存到磁盘后再操作,从而降低了效率。

而apply只是原子的提交到内容,后面有调用apply的函数的将会直接覆盖前面的内存数据,从一定程度上提高了效率。

建议

如果对提交的结果不关心的话,建议使用apply(),如果需要确保提交成功且有后续操作的话,还是需要用commit()。

多进程表现

是为了回答这个问题

一句话:在多进程中,如果要交换数据,不要使用SharedPreference,因为在不同版本表现不稳定,推荐使用ContentProvider替代。

在有的文章中,有提到在多进程中使用SharedPreference添加下面标志位就可以了。

MODE_MULTI_PROCESS

但是在官方文档中这样提到:

 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.

@deprecated 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

简单解释下,就是这个标志位在2.3之前是默认支持的,但是在2.3之后,如果需要多进程访问的情景,就需要显示的声明出来。

现在这个标志位被废弃了,因为在某些版本上表现不稳定。我们开发者不应该尝试去使用它,因为他没有提供任何并发机制,我们应该使用一种明确支持跨进程访问的机制,比如ContentProvider。

使用细节

更多参考资料

Copyright© 2013-2020

All Rights Reserved 京ICP备2023019179号-8