What the difference between commit() and apply() in Sharedpreferences in Android?


Introduction

Many times while building an android application. We came across a scenario where we have to store some of the data inside our mobile application. Such as the applications that store the session of the user. Android provides several different ways which we can use to store the data within an android application such as SQlite database, Sharedpreferences, Room Database and others. Each data storing method is used for a different purpose of data storing. If we have to store data in the form of tables then we prefer using Room database or SQlite Database.

What is Sharedpreferences in Android?

Shared Preference is a data storage class in android which stores data in the form of key and value pairs. With the help of shared preferences we can store the data as a key and value pair in android which will allow us for the storage and retrieval of primitive data types. It is a light weight, persistent data store that can be used to store application level data. It is used to store small pieces of information such as user preferences, application settings and other small pieces of data such as user login credentials, session id of the user and others. There are several methods which are used in Shared Preferences for storing and retrieval of data within android applications such as commit() apply(), put(), get() and others. In this article we will take a look on the mostly use methods in Shared Preferences such as commit() and apply()

Commit() and apply() are the two important methods used in Sharedpreferences class which are used to store the data in the preferences file. Both of these methods are used to store the data in the shared preferences file but each method has some key differences that are important to understand.

Commit()

The commit() method is the traditional way of storing data into the Shared Preference. It is a synchronous method, which means it will block the main thread until the data is stored in the Shared Preference. This can cause a delay in the user experience since the method will take a few milliseconds to complete. Also, the commit() method must return a boolean value to indicate whether the data was successfully stored or not.

Apply()

The apply() method was introduced in API level 9 (Android 2.3). It is an asynchronous method, which means it will not block the main thread, resulting in a better user experience. It also allows for multiple data to be stored at once, which is more efficient compared to the commit() method. Unlike the commit() method, the apply() method does not return a boolean value, so there is no way of knowing for sure whether the data was stored successfully or not.

Difference between Commit and Apply

Commit()

Apply()

Commit method sets the value of a preference and saves it synchronously.

Apply method saves the value of the preference and saves it asynchronously.

Return type of this method is a boolean. It might be true/false.

The return type of this method is a void.

This method blocks the main thread.

This method do not blocks the main thread.

Commit method will provide bad user experience as this method will take few milliseconds to complete/

Apply method runs asynchronously due to which the user experience is not affected.

Commit method return true or false, this will help to indicate whether the data is been stored in the shared preferences or not

Apply method returns a void which do not give any information weather the data is stored in the shared preferences nor not.

Conclusion

Both the commit() and apply() methods are used to store data into the Shared Preference. The commit() method is a synchronous method, which can cause a delay in the user experience, while the apply() method is an asynchronous method, which is more efficient and does not block the main thread. The commit() method must return a boolean value to indicate success, while the apply() method does not. It is important to understand the difference between the two methods before using them to store data.

Updated on: 30-Mar-2023

1K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements