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

AndroidSharedpreferences

Android Problem Overview


I am using SharedPreferences 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 Solutions


Solution 1 - Android

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

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

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.

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

Solution 2 - Android

tl;dr:

  • 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.

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

Solution 3 - Android

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.

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.

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

Solution 4 - Android

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.

Solution 5 - Android

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

  • apply() is void function.

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

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

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

Solution 6 - Android

The docs give a pretty good explanation of the difference between apply() and 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.

Solution 7 - Android

From 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

Solution 8 - Android

> The difference between commit() and apply()

We might be confused by those two terms, when we are using SharedPreference. Basically they are probably the same, so let’s clarify the differences of commit() and apply().

> 1.Return value:

apply() commits without returning a boolean indicating success or failure. commit() returns true if the save works, false otherwise.

> 2. Speed:

apply() is faster. commit() is slower.

> 3. Asynchronous v.s. Synchronous:

apply(): Asynchronous commit(): Synchronous

> 4. Atomic:

apply(): atomic commit(): atomic

> 5. Error notification:

apply(): No commit(): Yes

Solution 9 - Android

apply() changes the in-memory SharedPreferences object immediately but writes the updates to disk asynchronously.

commit() to write the data to disk synchronously. But because commit() is synchronous, you should avoid calling it from your main thread because it could pause your UI rendering.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionAndro SelvaView Question on Stackoverflow
Solution 1 - AndroidRay BrittonView Answer on Stackoverflow
Solution 2 - AndroidLukas KnuthView Answer on Stackoverflow
Solution 3 - AndroidJoseLSeguraView Answer on Stackoverflow
Solution 4 - AndroidMustafaView Answer on Stackoverflow
Solution 5 - AndroidNurlan SofiyevView Answer on Stackoverflow
Solution 6 - AndroidMojo RisinView Answer on Stackoverflow
Solution 7 - AndroidVladimir IvanovView Answer on Stackoverflow
Solution 8 - AndroidChanaka WeerasingheView Answer on Stackoverflow
Solution 9 - Androidforoogh VarmazyarView Answer on Stackoverflow