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
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.
- Speed:
apply()
is faster.
commit()
is slower.
- Asynchronous v.s. Synchronous:
apply()
: Asynchronous
commit()
: Synchronous
- Atomic:
apply()
: atomic
commit()
: atomic
- Error notification:
apply()
: No
commit()
: Yes
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