Data storage with Data Store

Tram Ho

Data Store is one of the components of JetPack Android library, it is a perfect alternative to SharedPreferences to store simple data as key-value. Let’s make a small comparison to see the optimization of the Data Store with SharedPreferences

If you do not know, SharedPreferences supports the handling of read and write jobs right on the UI thread. Its apply () method will block the UI thread on fsync (). The UI blocking is the main cause of the ANR (Application not responding) error. And it has the potential to lead to a bad experience for the end user. Reading and writing data with SharedPreferences can only detect an error during run time => another potential risk of incident. However, with the Data Store the risks above have been eliminated. With the use of Kotlin coroutines and Flow, the entire data read and write process will be processed asynchronously. In addition, Data Store also supports the control of exceptions in data read and write process. Note: Data Store is suitable for storing simple structured data. If you need partial updates, referential integrity or support for large, complex data sets, consider using Room Database.

1. Implement

DataStore provides two implementations: Preferences DataStore and Proto DataStore.

  • DataStore Preferences : Similar to SharedPreference, data will be saved as key-value as separate and unstructured tuples, nor does it provide type safety.
  • Proto DataStore : Storing structured data, simply understood as allowing you to store an object. You need to define a schema using protocol buffers . And it provides type safety

Install DataStore

Like other libraries, we need to add DataStore dependence to the Gradle module app

In addition, we need to add some other dependencies to be able to use Proto language as well as genarate objects.

2. DataStore Preferences

As the name suggests, the DataStore Preferences use the DataStore and Preferences to store key-value pairs to disk. As mentioned above, each key-value pair will be a separate record, suitable for storing single information such as first login, first time on app, … To create Preferences DataStore them. We do the following:

With the above command, we have created a data store called settings (this name you can put your mind)

2.1 Record data

The DataStore preferences provide an edit () method that allows us to create and update data

Similar to SharedPreference, we will access values ​​through predefined keys

2.2 Reading Data

The DataStore returns the Flow value

3 Proto DataStore

Sometimes we want to store information of an object with a simple structure like some information of a user. However, implementing a database to store only one user seems very time-consuming, while storing each user’s information will also take a lot of work in reading and writing data. Proto Data Store will help us to solve this. Proto DataStore uses DataStore and Proto buffers to store data to disk

3.1 Define a schema

Proto Data Store uses Proto language to define Schema. To create a Schema, you file “name_database” .proto to the / main / proto directory. The proto folder is not available, so you need to create this folder. Next, open the .proto file and define the objects you need to store.

To define an object, you use the private message key. The Data Store will genarate the corresponding objects. As in the example, a UserPreferences object will be created. Next, we need to tell the Data Store how to read and write the data defined in the proto file.

Note: The objects of proto are genarate at complie time, so you need to rebuild the project otherwise you won’t be able to find the objects in the database

3.2 Read and write data with DataStore Proto

3.2.1 Create data archive file

First, we need to create a Data Store to store the database. Use the createDataSource method to create a DataStore

  • fileName : The file name to store the database, this file name does not need to be the same as the .proto file name (a file used to store the database, a file used to define the schema). You can find this file under the path data / data / your_package / datastore / files
  • serializer : How DataStore will read and write data. The example above is the UserPreferencesSerializer object itself

Read the data

As mentioned above, the DataStore will return coroutine Flow, so we can easily controll the return data or errors during interaction with the database.

With exception handling will help prevent errors that can cause app crashes, as well as handle the UI when encountering error cases more easily.

Record data

To udpate the data we use the updateData method. This method will return the object after being updated

A UserPreferences object will be returned, we can access its properties with the changed value.


The Data Store is really a good alternative to SharedPreferences. Currently, the migration from SharedPreferences to StoreData is also very simple. So what are you waiting for without using the Data Store !!!

Share the news now

Source : Viblo