— SharedPreferences, Commit, Apply — 2 min read
When working with persistent data storage in Android development, SharedPreferences is a popular choice. It allows you to store key-value pairs that persist even when the app is closed. However, there are two different methods for saving changes made to SharedPreferences: commit
and apply
. In this article, we will explore the differences between these two methods and provide examples to help you understand when to use each.
The commit
method in SharedPreferences saves changes synchronously, blocking the execution until the changes are written to disk. Here's an example of how you can use commit
:
1val sharedPreferences = getSharedPreferences("my_prefs", Context.MODE_PRIVATE)2val editor = sharedPreferences.edit()3editor.putString("key", "value")4val isSaved = editor.commit()
In this example, we retrieve the SharedPreferences object and create an editor to make modifications. We then use the putString
method to store a key-value pair. Finally, we call commit
on the editor, which returns a boolean indicating whether the changes were successfully written to disk.
One advantage of using commit
is that it provides immediate feedback on the success or failure of the operation. However, since it blocks the execution, it can introduce performance issues if called from the main (UI) thread, especially when dealing with larger amounts of data.
The apply
method in SharedPreferences saves changes asynchronously, avoiding any blocking of the execution. It returns immediately and schedules the changes to be written in the background. Here's an example of how you can use apply
:
1val sharedPreferences = getSharedPreferences("my_prefs", Context.MODE_PRIVATE)2val editor = sharedPreferences.edit()3editor.putString("key", "value")4editor.apply()
In this example, the usage is similar to commit
, but we call apply
instead. As a result, there is no return value indicating the success or failure of the operation. The changes are scheduled to be written in the background, allowing the application to continue its execution without delay.
The advantage of using apply
is that it avoids blocking the main thread, ensuring smooth user experience even when dealing with larger data sets. However, keep in mind that there is no immediate feedback on whether the changes were successfully persisted.
When deciding between commit
and apply
, consider the following factors:
Synchronous vs Asynchronous: If you need immediate feedback on the success or failure of the operation, use commit
. If you want to avoid blocking the execution and prioritize a smoother user experience, use apply
.
Performance Impact: Since commit
blocks the execution, it can impact performance, especially on the main thread. For larger data sets or frequent updates, apply
is generally preferred.
Thread Safety: Both methods are thread-safe, meaning multiple threads can access SharedPreferences simultaneously without issues. However, if multiple edits are being performed concurrently, there is a possibility of one edit overwriting another. To avoid conflicts, consider using synchronization techniques like locks or AtomicBoolean.
In summary, commit
provides immediate feedback but can introduce performance issues, while apply
writes changes asynchronously, prioritizing smooth execution. Choose the method that aligns with your specific requirements and trade-offs.