Proto DataStore Jetpack is more secure than SharedPreferences.

Tram Ho


Before going into Proto DataStore, I will talk about DataStore. DataStore is a new and improved data storage solution aimed at replacing SharedPreferences. Built on top of Kotlin coroutines and Flow, DataStore offers two different implementations: Proto DataStore, which allows you to store imported objects (supported by Proto-protocal) and Preferences DataStore that stores in key pairs – value. Data is stored asynchronously, consistently and is transactional, overcoming some disadvantages of SharedPreferences.

In the scope of this article, I will only talk about Proto Data Store. People can refer to Google’s codelab about the DataStore Preferences here:

A comparison table of the 3 types of SharedPreferences, Preference DataStore and Proto DataStore.

Proto DataStore

One of the downsides of SharedPreferences and DataStore Preferences is that there is no way to define a schema or to ensure that the keys are accessed with the correct type. Proto DataStore solves this problem by using Protocol buffers to define the schema. Using Proto DataStore knows what types are stored and will only deliver them, eliminating the need to use keys.

  • It stores versions as custom data.
  • Define the schema using the Protocol Buffer.
  • They are faster, smaller, simpler, and less ambiguous than XML and other similar data formats.

Add Proto DataStore into Project:

This demo project is about arranging tasks according to the priority of the User chooses.

Go to ** build.gradle ** to add the code below.

Protocol buffers are a mechanism for serializing structured data. You define how you want your data to be structured once, and then the compiler generates the source code to make it easier to write and read the structured data. Create a Proto file: You define your schema in a proto file. In my project, I will create a file user_prefs.proto in the following path ** app / src / main / proto ** in this file with the following content: Each structure is defined by the keyword message and each member of The structure is defined within the message, based on the type and name, and it is assigned an order.

The UserPreferences class is created at compile time from the message specified in the proto file. Should rebuild when creating proto file.

Create serializer

In order for the DataStore to know how to read and write the data type that we specified in the proto file, we need to implement Serializer. Create a new file called UserPreferencesSerializer.

If UserPreferences or related methods are not found, Clean and Rebuild to make sure that Protobuf creates the object.

Create DataStore

Create the DataStore <UserPreferences> in UserPreferencesRepository based on the Context.createDataStore (). extension method Context.createDataStore (). The method has two required parameters:

  • The name of the file where the DataStore will be active.
  • Sequencer for the type to be used with the DataStore. In this project is UserPreferencesSerializer .

Read and write data from Proto DataStore

In UserPreferencesRepository will manage the reading and writing of data.

  • Read data:

  • Data logging: To write data, DataStore provides a suspend DataStore.updateData () function.

Move from SharedPreferences

To help with migration, the DataStore identifies the SharedPreferencesMigration class. Create it in UserPreferencesRepository . The moving block gives us two parameters:

  • SharedPreferencesView allows us to retrieve data from SharedPreferences.
  • Current data of UserPreferences .

Stores the order to the DataStore

To update the sort order when enableSortByDeadlin() and enableSortByPooter() are called, we have to do the following: Call their respective functions in the lambda of dataStore.updateData () . Since UpdateData () is a function suspend , should enableSortByDeadline() and enableSortByPooter() must also be made into a suspend . Use existing UserPreferences received from updateData() to build a new sort order Update UserPreferences by converting it to a generator, setting a new sort order, and then rebuilding the option. Here’s how the implementation of enableSortByDeadline() looks like.

The functions enableSortByDeadline() and enableSortByPooter() are currently suspend function so they will also be called in a new investigative program, launched in viewModelScope:


SharedPreferences comes with a bunch of downsides – from a synchronous API that can prove safe to call on the UI chain, no error signaling mechanism, lack of a transaction API, and more. The DataStore is an alternative to SharedPreferences that addresses most of the shortcomings of the API. DataStore has a completely asynchronous API that uses Kotlin coroutines and Flow, handles data movement, ensures data consistency, and handles data errors.

Thank you for reading this article.

Link refer to the article

Source code:

Share the news now

Source : Viblo