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

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
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 apply-calls and its own commit are finished.

Comments

Popular posts from this blog

Styling Bottom Navigation

Lottie Animation in Android

Set Up the AWS Mobile SDK for Android