在共享首选项中,commit()和apply()有什么区别

时间:2021-11-08 02:23:12

I am using shared preference in my android app. I am using both commit() and apply() method from shared preference. When I use AVD 2.3 it shows no error, but when I run the code in AVD 2.1, apply() method shows error. SO what's the difference between these two? And by using only commit() can I store the preference value without any problem?

我正在我的android应用程序中使用共享首选项。我在共享首选项中使用了commit()和apply()方法。当我使用AVD 2.3时,它没有显示任何错误,但是当我在avd2.1中运行代码时,apply()方法会显示错误。这两者有什么区别呢?通过只使用commit(),我可以毫无问题地存储首选项值吗?

7 个解决方案

#1


554  

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

apply()在2.3中添加,它提交时没有返回指示成功或失败的布尔值。

commit() returns true if the save works, false otherwise.

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

apply() was added as the Android dev team noticed that almost no one took notice of the return value, so apply is faster as it is asynchronous.

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

http://developer.android.com/reference/android/content/SharedPreferences.Editor.html#apply()

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

#2


169  

tl;dr:

tl;博士:

  • commit() writes the data synchronously (blocking the thread its called from). It then informs you about the success of the operation.
  • commit()同步写入数据(阻塞其调用的线程)。然后它会告诉你手术的成功。
  • apply() schedules the data to be written asynchronously. It does not inform you about the success of the operation.
  • apply()调度异步写入的数据。它没有告诉你这次行动的成功。
  • If you save with apply() and immediately read via any getX-method, the new value will be returned!
  • 如果您使用apply()保存并立即通过任何getx方法读取,新的值将被返回!
  • 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.
  • 如果您在某个时间点调用apply(),并且它仍在执行,任何提交()的调用都会阻塞,直到所有过去的应用程序调用和当前的提交调用完成。

More in-depth information from the SharedPreferences.Editor Documentation:

来自共享首选项的更深入的信息。编辑文档:

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()将其首选项以同步方式写入持久存储中,apply()将其更改立即提交给内存*享的首选项,但启动对磁盘的异步提交,因此不会收到任何失败的通知。如果这个SharedPreferences上的另一个编辑器执行常规的commit(),而apply()仍然是未完成的,那么commit()将阻塞,直到所有异步提交完成以及提交本身完成为止。

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.

由于SharedPreferences实例是进程中的单例,所以如果您已经忽略了返回值,那么可以将commit()的任何实例替换为apply()。

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

SharedPreferences。编辑器接口预计不会直接实现。但是,如果您以前确实实现了它,并且现在遇到了关于缺少apply()的错误,您可以简单地从apply()调用commit()。

#3


16  

I'm experiencing some problems using apply() instead commit(). As stated before in other responses, the apply() is asynchronous. I'm getting the problem that the changes formed to a "string set" preference are never written to the persistent memory.

我正在使用apply()而不是commit()来体验一些问题。如前所述,在其他响应中,apply()是异步的。我遇到了这样的问题:形成“string set”首选项的更改从未写入持久内存。

It happens if you "force detention" of the program or, in the ROM that I have installed on my device with Android 4.1, when the process is killed by the system due to memory necessities.

如果你对程序进行“强制拘留”,或者在我的设备上安装了Android 4.1系统,当系统因内存需要而被系统杀死时,就会发生这种情况。

I recommend to use "commit()" instead "apply()" if you want your preferences alive.

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

#4


10  

Use apply().

使用适用于()。

It writes the changes to the RAM immediately and waits and writes it to the internal storage(the actual preference file) after. Commit writes the changes synchronously and directly to the file.

它立即将更改写入RAM,然后等待并将其写入内部存储(实际的首选项文件)。Commit将更改同步并直接写入文件。

#5


10  

The docs give a pretty good explanation of the difference between apply() and commit():

文档说明了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()将其首选项以同步方式写入持久存储中,apply()将其更改立即提交给内存*享的首选项,但启动对磁盘的异步提交,因此不会收到任何失败的通知。如果这个SharedPreferences上的另一个编辑器执行常规的commit(),而apply()仍然是未完成的,那么commit()将阻塞,直到所有异步提交完成以及提交本身完成为止。由于SharedPreferences实例是进程中的单例,所以如果您已经忽略了返回值,那么可以将commit()的任何实例替换为apply()。

#6


8  

  • commit() is synchronously, apply() is asynchronous

    commit()是同步的,apply()是异步的

  • apply() is void function.

    应用()函数无效。

  • commit() returns true if the new values were successfully written to persistent storage.

    如果将新值成功写入持久存储,则commit()返回true。

  • apply() guarantees complete before switching states , you don't need to worry about Android component lifecycles

    在切换状态之前应用()保证,您无需担心Android组件的生命周期。

If you dont use value returned from commit() and you're using commit() from main thread, use apply() instead of commit()

如果您不使用从commit()返回的值,并且正在使用来自主线程的commit(),那么使用apply()而不是commit()

#7


4  

From javadoc:

从javadoc:

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()将其首选项以同步方式写入持久存储中,apply()将其更改立即提交给内存*享的首选项,但启动对磁盘的异步提交,因此不会收到任何失败的通知。如果这个SharedPreferences上的另一个编辑器执行常规的commit(),而> apply()仍然是未完成的,那么commit()将阻塞,直到所有异步提交完成以及提交本身完成为止

#1


554  

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

apply()在2.3中添加,它提交时没有返回指示成功或失败的布尔值。

commit() returns true if the save works, false otherwise.

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

apply() was added as the Android dev team noticed that almost no one took notice of the return value, so apply is faster as it is asynchronous.

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

http://developer.android.com/reference/android/content/SharedPreferences.Editor.html#apply()

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

#2


169  

tl;dr:

tl;博士:

  • commit() writes the data synchronously (blocking the thread its called from). It then informs you about the success of the operation.
  • commit()同步写入数据(阻塞其调用的线程)。然后它会告诉你手术的成功。
  • apply() schedules the data to be written asynchronously. It does not inform you about the success of the operation.
  • apply()调度异步写入的数据。它没有告诉你这次行动的成功。
  • If you save with apply() and immediately read via any getX-method, the new value will be returned!
  • 如果您使用apply()保存并立即通过任何getx方法读取,新的值将被返回!
  • 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.
  • 如果您在某个时间点调用apply(),并且它仍在执行,任何提交()的调用都会阻塞,直到所有过去的应用程序调用和当前的提交调用完成。

More in-depth information from the SharedPreferences.Editor Documentation:

来自共享首选项的更深入的信息。编辑文档:

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()将其首选项以同步方式写入持久存储中,apply()将其更改立即提交给内存*享的首选项,但启动对磁盘的异步提交,因此不会收到任何失败的通知。如果这个SharedPreferences上的另一个编辑器执行常规的commit(),而apply()仍然是未完成的,那么commit()将阻塞,直到所有异步提交完成以及提交本身完成为止。

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.

由于SharedPreferences实例是进程中的单例,所以如果您已经忽略了返回值,那么可以将commit()的任何实例替换为apply()。

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

SharedPreferences。编辑器接口预计不会直接实现。但是,如果您以前确实实现了它,并且现在遇到了关于缺少apply()的错误,您可以简单地从apply()调用commit()。

#3


16  

I'm experiencing some problems using apply() instead commit(). As stated before in other responses, the apply() is asynchronous. I'm getting the problem that the changes formed to a "string set" preference are never written to the persistent memory.

我正在使用apply()而不是commit()来体验一些问题。如前所述,在其他响应中,apply()是异步的。我遇到了这样的问题:形成“string set”首选项的更改从未写入持久内存。

It happens if you "force detention" of the program or, in the ROM that I have installed on my device with Android 4.1, when the process is killed by the system due to memory necessities.

如果你对程序进行“强制拘留”,或者在我的设备上安装了Android 4.1系统,当系统因内存需要而被系统杀死时,就会发生这种情况。

I recommend to use "commit()" instead "apply()" if you want your preferences alive.

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

#4


10  

Use apply().

使用适用于()。

It writes the changes to the RAM immediately and waits and writes it to the internal storage(the actual preference file) after. Commit writes the changes synchronously and directly to the file.

它立即将更改写入RAM,然后等待并将其写入内部存储(实际的首选项文件)。Commit将更改同步并直接写入文件。

#5


10  

The docs give a pretty good explanation of the difference between apply() and commit():

文档说明了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()将其首选项以同步方式写入持久存储中,apply()将其更改立即提交给内存*享的首选项,但启动对磁盘的异步提交,因此不会收到任何失败的通知。如果这个SharedPreferences上的另一个编辑器执行常规的commit(),而apply()仍然是未完成的,那么commit()将阻塞,直到所有异步提交完成以及提交本身完成为止。由于SharedPreferences实例是进程中的单例,所以如果您已经忽略了返回值,那么可以将commit()的任何实例替换为apply()。

#6


8  

  • commit() is synchronously, apply() is asynchronous

    commit()是同步的,apply()是异步的

  • apply() is void function.

    应用()函数无效。

  • commit() returns true if the new values were successfully written to persistent storage.

    如果将新值成功写入持久存储,则commit()返回true。

  • apply() guarantees complete before switching states , you don't need to worry about Android component lifecycles

    在切换状态之前应用()保证,您无需担心Android组件的生命周期。

If you dont use value returned from commit() and you're using commit() from main thread, use apply() instead of commit()

如果您不使用从commit()返回的值,并且正在使用来自主线程的commit(),那么使用apply()而不是commit()

#7


4  

From javadoc:

从javadoc:

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()将其首选项以同步方式写入持久存储中,apply()将其更改立即提交给内存*享的首选项,但启动对磁盘的异步提交,因此不会收到任何失败的通知。如果这个SharedPreferences上的另一个编辑器执行常规的commit(),而> apply()仍然是未完成的,那么commit()将阻塞,直到所有异步提交完成以及提交本身完成为止