Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference Between commit and apply in Android SharedPreferences [duplicate]

Tags:

SharedPreferences are used to save Application data in Android.

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

As mentioned in Android Library:

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.

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.

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

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.

like image 684
Veer Shrivastav Avatar asked Mar 11 '13 09:03

Veer Shrivastav


People also ask

What is the difference between commit () and apply () method in SharedPreferences ()?

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.

Can we store large amount of data in SharedPreferences?

SharedPreferences are not intended to store a lot of data, there is no limit per se (since it is an xml file), but for larger sets of data, I would suggest using Room (or SQLite for the older projects). There is also another reason why storing in a database makes more sense.

What is the method used to store and retrieve data on the SharedPreferences?

Shared Preferences allow you to save and retrieve data in the form of key,value pair. In order to use shared preferences, you have to call a method getSharedPreferences() that returns a SharedPreference instance pointing to the file that contains the values of preferences.


2 Answers

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

like image 98
fedepaol Avatar answered Sep 28 '22 07:09

fedepaol


apply() - returns void

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

commit() - returns boolean value.

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.

You can refer to below link

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

like image 34
Nirali Avatar answered Sep 28 '22 07:09

Nirali