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

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


当前回答

Apply()是在2.3中添加的,它提交时不返回一个表示成功或失败的布尔值。

如果保存有效,Commit()返回true,否则返回false。

添加apply()是因为Android开发团队注意到几乎没有人注意到返回值,所以apply更快,因为它是异步的。

http://developer.android.com/reference/android/content/SharedPreferences.Editor.html应用()

其他回答

文档很好地解释了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.

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().

从javadoc:

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

commit()和apply()的区别

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

1.返回值:

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

速度:

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

异步vs .同步:

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

原子性:

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

错误通知:

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

Commit()是同步的,apply()是异步的 Apply()是void函数。 如果新值成功写入持久存储,Commit()返回true。 apply()保证在切换状态之前完成,你不需要担心Android组件的生命周期

如果你不使用commit()返回的值,而你在主线程中使用commit(),请使用apply()而不是commit()