java Android SharedPreferences 中 commit 和 apply 的区别

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15335456/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 19:16:57  来源:igfitidea点击:

Difference Between commit and apply in Android SharedPreferences

javaandroidsharedpreferences

提问by Veer Shrivastav

SharedPreferences are used to save Application data in Android.

SharedPreferences 用于在 Android 中保存应用程序数据。

commit()and apply()both are used to save the changes in the shared preferences.

commit()apply()两者都用来保存在共享偏好的变化。

As mentioned in Android Library:

如 Android 库中所述:

public abstarct void apply():

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.

与将其首选项同步写入持久存储的 commit() 不同,apply() 立即将其更改提交到内存中的 SharedPreferences,但会启动对磁盘的异步提交,并且您不会收到任何失败通知。如果此 SharedPreferences 上的另一个编辑器在 apply() 仍然未完成时执行常规 commit(),则 commit() 将阻塞,直到完成所有异步提交以及提交本身。

public abstract boolean commit ():

Commit your preferences changes back from this Editor to the SharedPreferences object it is editing. This atomically performs the requested modifications, replacing whatever is currently in the SharedPreferences.

将此编辑器的首选项更改提交回它正在编辑的 SharedPreferences 对象。这会自动执行请求的修改,替换当前 SharedPreferences 中的任何内容。

Does this mean that the changes made by commit()are instant as compared with apply()? Which one is better?

这是否意味着commit()与 相比所做的更改是即时的apply()?哪一个更好?

If I need to use the same shared preference value in the next immediate activity, which one should I use? As I have seen if the value of Preference is updated it is not reflected till the application is restarted.

如果我需要在下一个即时活动中使用相同的共享首选项值,我应该使用哪个?正如我所看到的,如果 Preference 的值被更新,它在应用程序重新启动之前不会反映出来。

采纳答案by fedepaol

Commit()is instantaneous but performs disk writes. If you are on the ui thread you should call apply()which is asynchronous.

Commit()是瞬时的,但执行磁盘写入。如果您在 ui 线程上,您应该调用apply()它是异步的。

回答by Nirali

apply()- returns void

apply()- 返回无效

apply() was added in 2.3, it saves without returning a boolean indicating success or failure.

apply() 是在 2.3 中添加的,它保存而不返回指示成功或失败的布尔值。

commit()- returns booleanvalue.

commit()- 返回布尔值。

commit()returns true if the save works, false otherwise. apply()was added as the android dev team noticed that most no one took notice of the return value, so apply is faster.

如果保存有效,则commit()返回 true,否则返回 false。apply()添加是因为 android 开发团队注意到大多数人都没有注意到返回值,因此 apply 更快。

You can refer to below link

你可以参考以下链接

What's the difference between commit() and apply() in Shared Preference

Shared Preference 中的 commit() 和 apply() 有什么区别