Professional Documents
Culture Documents
All About Preferences DataStore. in This Post, We Will Take A Look at - by Simona Milanović - Android Developers - Medium
All About Preferences DataStore. in This Post, We Will Take A Look at - by Simona Milanović - Android Developers - Medium
aStore. In this post, we will take a look at… | by Simona Milanović | Android Devel…
Search
Listen Share
In this post, we will take a look at Preferences DataStore, one of two DataStore
implementations. We will go over how to create it, read and write data, and how to
handle exceptions, all of which should, hopefully, provide you with enough
information to decide if it’s the right choice for your app.
To quickly recap:
Provides a fully asynchronous API for retrieving and saving data, using the
power of Kotlin coroutines
https://medium.com/androiddevelopers/all-about-preferences-datastore-cc7995679334 1/16
31/03/2024, 11:50 All about Preferences DataStore. In this post, we will take a look at… | by Simona Milanović | Android Devel…
Does not offer ready-to-use synchronous support — it directly avoids doing any
work that blocks the UI thread
Relies on Flow’s inner error signaling mechanism, allowing you to safely catch
and handle exceptions when reading or writing data
Wish to quickly migrate from SharedPreferences with minimal changes and feel
confident enough without full type safety? Choose Preferences over Proto
Now let’s dive into some code and learn how Preferences should be implemented.
We will use the Preferences DataStore codelab code. If you’re interested in a more
hands-on approach with implementation, we really encourage you to go through the
Working with Preferences DataStore codelab on your own.
This sample app displays a list of tasks and the user can choose to filter them by
their completed status or sort by priority and deadline. We want to store their
selection — a boolean for completed tasks and a sort order enum in DataStore.
Dependency setup
Let’s start with adding the necessary dependency:
💡 Quick tip — if you want to minify your build, make sure to add an additional rule
to your proguard-rules.pro file to prevent your fields from being deleted:
https://medium.com/androiddevelopers/all-about-preferences-datastore-cc7995679334 2/16
31/03/2024, 11:50 All about Preferences DataStore. In this post, we will take a look at… | by Simona Milanović | Android Devel…
and pass a mandatory name argument. This delegate is a Kotlin extension property
whose receiver type must be an instance of Context , subsequently needed for
constructing the File object, where DataStore stores the data:
You shouldn’t create more than one instance of DataStore for a given file, as doing
so can break all DataStore functionality. Therefore, you can add the delegate
construction once at the top level of your Kotlin file and use it throughout your
application, in order to pass it as a singleton. In later posts, we will see how to do
this with dependency injection.
Defining keys
DataStore provides a quick way of constructing keys for different data types, such as
booleanPreferencesKey , intPreferencesKey and many more — you just need to pass
the key name as value. While this does put some constraints on data types, keep in
https://medium.com/androiddevelopers/all-about-preferences-datastore-cc7995679334 3/16
31/03/2024, 11:50 All about Preferences DataStore. In this post, we will take a look at… | by Simona Milanović | Android Devel…
mind that it doesn’t provide definite type safety. By specifying a preference key of a
certain type, we hope for the best and rely on our assumptions that a value of a
certain type would be returned. If you feel your code is structured in a way to
handle this safely, feel free to continue with Preferences. If not, consider using
Preferences’ sibling, Proto DataStore, as it provides full type safety.
Reading data
To read the stored data, in UserPreferencesRepository we expose a Flow<Preferences>
from dataStore.data . This provides efficient access to the latest saved state and
emits with every change. Using Kotlin data classes, we can observe any emissions
and transform the provided DataStore Preferences object into our own
UserPreferences model, using only the key-value pairs we’re interested in:
The Flow will always either emit a value or throw an exception when attempting to
read from disk. We will look at exception handling in later sections. DataStore also
https://medium.com/androiddevelopers/all-about-preferences-datastore-cc7995679334 4/16
31/03/2024, 11:50 All about Preferences DataStore. In this post, we will take a look at… | by Simona Milanović | Android Devel…
🚨 Do not create any cache repositories to mirror the current state of your
Preferences data. Doing so would invalidate DataStore’s guarantee of data
consistency. If you require a single snapshot of your data without subscribing to
further Flow emissions, prefer using dataStore.data.first() :
Writing data
For writing data, we will use a suspend DataStore<Preferences>.edit(transform:
https://medium.com/androiddevelopers/all-about-preferences-datastore-cc7995679334 5/16
31/03/2024, 11:50 All about Preferences DataStore. In this post, we will take a look at… | by Simona Milanović | Android Devel…
🚨 Keep in mind that this is the only way of making changes to the DataStore state.
Keeping a MutablePreferences reference and mutating it manually after transform
completes will not change the persisted data in DataStore, so you shouldn’t attempt
to modify MutablePreferences outside of the transform block.
If the writing operation fails for any reason, the transaction is aborted and an
exception is thrown.
https://medium.com/androiddevelopers/all-about-preferences-datastore-cc7995679334 6/16
31/03/2024, 11:50 All about Preferences DataStore. In this post, we will take a look at… | by Simona Milanović | Android Devel…
Exception handling
One of the main advantages of DataStore over SharedPreferences is its neat
mechanism for catching and handling exceptions. While SharedPreferences throws
parsing errors as runtime exceptions, leaving room for unexpected, uncaught
crashes, DataStore throws an IOException when an error occurs with
reading/writing data.
We can safely handle this by using the catch() Flow operator right before map()
https://medium.com/androiddevelopers/all-about-preferences-datastore-cc7995679334 7/16
31/03/2024, 11:50 All about Preferences DataStore. In this post, we will take a look at… | by Simona Milanović | Android Devel…
To be continued
We’ve covered DataStore’s Preferences implementation — when and how to use it
for reading and writing data, how to handle errors and how to migrate from
SharedPreferences . In the next post, we will cover the same topics with the Proto
DataStore implementation, so stick around.
You can find all posts from our Jetpack DataStore series here:
Introduction to Jetpack DataStore
https://medium.com/androiddevelopers/all-about-preferences-datastore-cc7995679334 8/16
31/03/2024, 11:50 All about Preferences DataStore. In this post, we will take a look at… | by Simona Milanović | Android Devel…
Follow
https://medium.com/androiddevelopers/all-about-preferences-datastore-cc7995679334 9/16
31/03/2024, 11:50 All about Preferences DataStore. In this post, we will take a look at… | by Simona Milanović | Android Devel…
271 4
Strong skipping mode changes the rules for what composables can skip recomposition and
should greatly reduce recomposition.
1K 7
951 3
https://medium.com/androiddevelopers/all-about-preferences-datastore-cc7995679334 11/16
31/03/2024, 11:50 All about Preferences DataStore. In this post, we will take a look at… | by Simona Milanović | Android Devel…
176 4
https://medium.com/androiddevelopers/all-about-preferences-datastore-cc7995679334 12/16
31/03/2024, 11:50 All about Preferences DataStore. In this post, we will take a look at… | by Simona Milanović | Android Devel…
Michihiro Iwasaki
66 1
https://medium.com/androiddevelopers/all-about-preferences-datastore-cc7995679334 13/16
31/03/2024, 11:50 All about Preferences DataStore. In this post, we will take a look at… | by Simona Milanović | Android Devel…
Lists
Staff Picks
609 stories · 872 saves
https://medium.com/androiddevelopers/all-about-preferences-datastore-cc7995679334 14/16
31/03/2024, 11:50 All about Preferences DataStore. In this post, we will take a look at… | by Simona Milanović | Android Devel…
Ivan Dimitrov
Manu Aravind
11 1
Ryan W
https://medium.com/androiddevelopers/all-about-preferences-datastore-cc7995679334 16/16