Professional Documents
Culture Documents
All About Proto DataStore. in This Post, We Will Learn About Proto - by Simona Milanović - Android Developers - Medium
All About Proto DataStore. in This Post, We Will Learn About Proto - by Simona Milanović - Android Developers - Medium
aStore. In this post, we will learn about Proto… | by Simona Milanović | Android Develop…
Search
Listen Share
In this post, we will learn about Proto DataStore, one of two DataStore
implementations. We will discuss how to create it, read and write data and how to
handle exceptions, to better understand the scenarios that make Proto a great
choice.
Proto DataStore uses typed objects backed by Protocol Buffers, to store smaller
datasets while providing type safety. It removes the need for using key-value pairs,
making it structurally different from its SharedPreferences predecessor and its
sibling implementation, Preferences DataStore. However, that’s not all — DataStore
brings many other improvements over SharedPreferences . Feel free to quickly jump
back to our first post in the series and take a look at the detailed comparison we’ve
made there. Going forward, we will refer to Proto DataStore as just Proto , unless
specified otherwise.
To sum up:
https://medium.com/androiddevelopers/all-about-proto-datastore-1b1af6cd2879 1/22
31/03/2024, 11:53 All about Proto DataStore. In this post, we will learn about Proto… | by Simona Milanović | Android Develop…
Provides a fully asynchronous API for retrieving and saving data, using the
power of Kotlin coroutines
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 signalling mechanism, allowing you to safely catch
and handle exceptions when reading or writing data
Need full type safety and your data requires working with more complex
classes, like enums or lists? This isn’t possible with Preferences, so choose Proto
instead
You define a schema of how you want your data to be structured and specify
options such as which language to use for code generation. The compiler then
generates classes according to your specifications. This allows you to easily write
and read the structured data to and from a variety of data streams, share between
different platforms, using a number of different languages, like Kotlin.
https://medium.com/androiddevelopers/all-about-proto-datastore-1b1af6cd2879 2/22
31/03/2024, 11:53 All about Proto DataStore. In this post, we will learn about Proto… | by Simona Milanović | Android Develop…
How to use the generated Kotlin code for constructing your data model:
Or you can try out the newly announced Kotlin DSL support for protocol buffers for
a more idiomatic way of building your data model:
Investing a bit more time into learning this new serialization mechanism is
definitely worth it as it brings type safety, improved readability and overall code
simplicity.
We will use the Proto DataStore codelab sample. If you’re interested in a more
hands-on approach with implementation, we really encourage you to go through the
Working with Proto 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 displaying completed tasks and a sort order enum in
Proto.
https://medium.com/androiddevelopers/all-about-proto-datastore-1b1af6cd2879 3/22
31/03/2024, 11:53 All about Proto DataStore. In this post, we will learn about Proto… | by Simona Milanović | Android Develop…
We will firstly add Proto dependencies and some of the basic protobuf settings to
your module’s build.gradle . If you’re interested in a more advanced customisation
of the protobufs compilation, check out the Protobuf Plugin for Gradle notes:
💡 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-proto-datastore-1b1af6cd2879 4/22
31/03/2024, 11:53 All about Proto DataStore. In this post, we will learn about Proto… | by Simona Milanović | Android Develop…
directory.
Following the Protobuf language guide, in this file we will add a message for each
data structure we want to serialize, then specify a name and a type for each field in
the message. To help visualize this, let’s look at both a Kotlin data class and a
corresponding protobuf schema.
https://medium.com/androiddevelopers/all-about-proto-datastore-1b1af6cd2879 5/22
31/03/2024, 11:53 All about Proto DataStore. In this post, we will learn about Proto… | by Simona Milanović | Android Develop…
If you haven’t used protobufs before, you might also be curious about the first few
lines in the schema. Let’s break them down:
java_package — file option that specifies package declaration for your generated
classes, which helps prevent naming conflicts between different projects
java_multiple_files — file option that specifies whether only a single file with
nested subclasses will be generated for this .proto (when set to false) or if
separate files will be generated for each top-level message type (when set to
true); it is false by default
int32 , float , double, and string . You can also add further structure to your
messages by using other message types as field types, like we did with SortOrder .
The = 1, = 2 markers on each element identify the unique “tag” that the field uses
in the binary encoding — like an ID of sort. Once your message type is in use, these
numbers should not be changed.
https://medium.com/androiddevelopers/all-about-proto-datastore-1b1af6cd2879 6/22
31/03/2024, 11:53 All about Proto DataStore. In this post, we will learn about Proto… | by Simona Milanović | Android Develop…
When you run the protocol buffer compiler on a .proto, the compiler generates the
code in your chosen language. In our specific case, when the compiler is run, this
leads to the generation of the UserPreferences class, found in your app’s
build/generated/source/proto … directory:
💡 Quick tip — You can also try out the newly announced Kotlin DSL support for
protocol buffers to use a more idiomatic way of building your data model.
Now that we have UserPreferences , we need to specify the guidelines for how Proto
should read and write them. We do this via the DataStore Serializer that
determines the final format of your data when stored and how to properly access it.
This requires overriding:
writeTo — how to transform the memory representation of our data object into
a format fit for storage
readFrom — inverse from the above, how to transform from a storage format into
a corresponding memory representation
https://medium.com/androiddevelopers/all-about-proto-datastore-1b1af6cd2879 7/22
31/03/2024, 11:53 All about Proto DataStore. In this post, we will learn about Proto… | by Simona Milanović | Android Develop…
related, clean and rebuild your project to initiate the generation of the protobuf
classes.
To create this instance, it is recommended to use the delegate dataStore and pass
mandatory fileName and serializer arguments:
https://medium.com/androiddevelopers/all-about-proto-datastore-1b1af6cd2879 8/22
31/03/2024, 11:53 All about Proto DataStore. In this post, we will learn about Proto… | by Simona Milanović | Android Develop…
fileName is used to create a File used to store the data. This is why the dataStore
Avoid using this file in any way outside of Proto, as it would break the consistency
of your data.
In the dataStore delegate, you can pass one more optional argument —
corruptionHandler . This handler is invoked if a CorruptionException is thrown by the
serializer when the data cannot be de-serialized. corruptionHandler would then
instruct Proto how to replace the corrupted 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.
Reading data
To read the stored data, in UserPreferencesRepository we expose a
Flow<UserPreferences> from userPreferencesStore.data . This provides efficient
access to the latest saved state and emits with every change. This is one of the
biggest strengths of Proto — your Flow ’s values already come in the shape of the
https://medium.com/androiddevelopers/all-about-proto-datastore-1b1af6cd2879 9/22
31/03/2024, 11:53 All about Proto DataStore. In this post, we will learn about Proto… | by Simona Milanović | Android Develop…
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
ensures that work is always performed on Dispatchers.IO so your UI thread isn’t
blocked.
🚨 Do not create any cache repositories to mirror the current state of your Proto
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 userPreferencesStore.data.first() :
Writing data
For writing data, we will use a suspend
DataStore<UserPreferences>.updateData(transform: suspend (t: T) -> T) function.
https://medium.com/androiddevelopers/all-about-proto-datastore-1b1af6cd2879 10/22
31/03/2024, 11:53 All about Proto DataStore. In this post, we will learn about Proto… | by Simona Milanović | Android Develop…
transform: suspend (t: T) -> T) — a suspend block used to apply the specified
changes to our persisted data of type T
Again, you might notice a difference to Preferences DataStore which relies on using
Preferences and MutablePreferences , similar to Map and MutableMap , as the default
data representation.
We can now use this to change our showCompleted boolean. Protocol buffers simplify
this as well, removing the need for any manual transformation from and to data
classes:
🚨 Keep in mind that this is the only way of making changes to the DataStore state.
Keeping a UserPreferences reference and mutating it manually after transform
https://medium.com/androiddevelopers/all-about-proto-datastore-1b1af6cd2879 11/22
31/03/2024, 11:53 All about Proto DataStore. In this post, we will learn about Proto… | by Simona Milanović | Android Develop…
completes will not change the persisted data in Proto, so you shouldn’t attempt to
modify UserPreferences outside of the transform block.
If the writing operation fails for any reason, the transaction is aborted and an
exception is thrown.
produceMigrations will ensure that the migrate() is run before any potential data
access to DataStore. This means your migration must have succeeded before
https://medium.com/androiddevelopers/all-about-proto-datastore-1b1af6cd2879 12/22
31/03/2024, 11:53 All about Proto DataStore. In this post, we will learn about Proto… | by Simona Milanović | Android Develop…
DataStore emits any further values and before it begins making any new changes to
the data. Once you’ve successfully migrated, it’s safe to stop using
SharedPreferences , as the keys are migrated only once and then removed from
SharedPreferences .
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 and emitting
getDefaultInstance() :
https://medium.com/androiddevelopers/all-about-proto-datastore-1b1af6cd2879 13/22
31/03/2024, 11:53 All about Proto DataStore. In this post, we will learn about Proto… | by Simona Milanović | Android Develop…
To be continued
We’ve covered Protocol Buffers and DataStore’s Proto 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 and final post, we will go a step further and
look at how DataStore fits in your app’s architecture, how to inject it with Hilt and
of course, how to test it. See you soon!
You can find all posts from our Jetpack DataStore series here:
Introduction to Jetpack DataStore
All about Preferences DataStore
All about Proto DataStore
DataStore and dependency injection
DataStore and Kotlin serialization
DataStore and synchronous work
DataStore and data migration
DataStore and testing
https://medium.com/androiddevelopers/all-about-proto-datastore-1b1af6cd2879 14/22
31/03/2024, 11:53 All about Proto DataStore. In this post, we will learn about Proto… | by Simona Milanović | Android Develop…
Follow
383 4
https://medium.com/androiddevelopers/all-about-proto-datastore-1b1af6cd2879 15/22
31/03/2024, 11:53 All about Proto DataStore. In this post, we will learn about Proto… | by Simona Milanović | Android Develop…
1K 7
Create a progress bar that transitions from a squiggly “star” shaped rounded polygon to a
circle while performing the regular progress…
951 3
271 4
https://medium.com/androiddevelopers/all-about-proto-datastore-1b1af6cd2879 17/22
31/03/2024, 11:53 All about Proto DataStore. In this post, we will learn about Proto… | by Simona Milanović | Android Develop…
Manu Aravind
11 1
https://medium.com/androiddevelopers/all-about-proto-datastore-1b1af6cd2879 18/22
31/03/2024, 11:53 All about Proto DataStore. In this post, we will learn about Proto… | by Simona Milanović | Android Develop…
Lists
Staff Picks
609 stories · 872 saves
https://medium.com/androiddevelopers/all-about-proto-datastore-1b1af6cd2879 19/22
31/03/2024, 11:53 All about Proto DataStore. In this post, we will learn about Proto… | by Simona Milanović | Android Develop…
Michihiro Iwasaki
66 1
https://medium.com/androiddevelopers/all-about-proto-datastore-1b1af6cd2879 20/22
31/03/2024, 11:53 All about Proto DataStore. In this post, we will learn about Proto… | by Simona Milanović | Android Develop…
2.7K 14
https://medium.com/androiddevelopers/all-about-proto-datastore-1b1af6cd2879 21/22
31/03/2024, 11:53 All about Proto DataStore. In this post, we will learn about Proto… | by Simona Milanović | Android Develop…
122
https://medium.com/androiddevelopers/all-about-proto-datastore-1b1af6cd2879 22/22