我在我的android应用程序中使用SharedPreferences。我同时使用commit()和apply()方法共享首选项。当我使用avd2.3时,它显示没有错误,但当我在avd2.1中运行代码时,apply()方法显示错误。

这两者有什么区别呢?通过只使用commit()我可以存储首选项值没有任何问题吗?


当前回答

我使用apply()而不是commit()遇到了一些问题。正如前面在其他响应中所述,apply()是异步的。我得到的问题是,形成一个“字符串集”偏好的变化从来没有写到持久内存。

如果你“强制扣留”程序,或者在我安装在Android 4.1设备上的ROM中,由于内存需求进程被系统杀死,就会发生这种情况。

如果你想激活你的首选项,我建议使用“commit()”而不是“apply()”。

其他回答

我使用apply()而不是commit()遇到了一些问题。正如前面在其他响应中所述,apply()是异步的。我得到的问题是,形成一个“字符串集”偏好的变化从来没有写到持久内存。

如果你“强制扣留”程序,或者在我安装在Android 4.1设备上的ROM中,由于内存需求进程被系统杀死,就会发生这种情况。

如果你想激活你的首选项,我建议使用“commit()”而不是“apply()”。

tl; diana:

commit() writes the data synchronously (blocking the thread its called from). It then informs you about the success of the operation. apply() schedules the data to be written asynchronously. It does not inform you about the success of the operation. If you save with apply() and immediately read via any getX-method, the new value will be returned! If you called apply() at some point and it's still executing, any calls to commit() will block until all past apply-calls and the current commit-call are finished.

更多深入的信息来自SharedPreferences。编辑文档:

Unlike commit(), which writes its preferences out to persistent storage synchronously, apply() commits its changes to the in-memory SharedPreferences immediately but starts an asynchronous commit to disk and you won't be notified of any failures. If another editor on this SharedPreferences does a regular commit() while a apply() is still outstanding, the commit() will block until all async commits are completed as well as the commit itself. As SharedPreferences instances are singletons within a process, it's safe to replace any instance of commit() with apply() if you were already ignoring the return value. The SharedPreferences.Editor interface isn't expected to be implemented directly. However, if you previously did implement it and are now getting errors about missing apply(), you can simply call commit() from apply().

文档很好地解释了apply()和commit()之间的区别:

Unlike commit(), which writes its preferences out to persistent storage synchronously, apply() commits its changes to the in-memory SharedPreferences immediately but starts an asynchronous commit to disk and you won't be notified of any failures. If another editor on this SharedPreferences does a regular commit() while a apply() is still outstanding, the commit() will block until all async commits are completed as well as the commit itself. As SharedPreferences instances are singletons within a process, it's safe to replace any instance of commit() with apply() if you were already ignoring the return value.

commit()和apply()的区别

当我们使用SharedPreference时,我们可能会被这两个术语混淆。基本上它们可能是相同的,因此让我们澄清commit()和apply()的区别。

1.返回值:

Apply()提交时不返回表示成功或失败的布尔值。 如果保存有效,Commit()返回true,否则返回false。

速度:

Apply()更快。 Commit()比较慢。

异步vs .同步:

应用():异步的 commit():同步

原子性:

应用():原子 commit():原子

错误通知:

应用():没有 commit():是的

从javadoc:

与commit()不同,commit()将其写入 首选项输出到持久存储 同时,apply()提交它的 内存中的更改 SharedPreferences,但是 启动对磁盘的异步提交 我不会通知你的 失败。如果这个SharedPreferences上的另一个编辑器执行常规的commit(),而> apply()仍然未完成,commit()将阻塞,直到所有异步提交以及提交本身完成