Learn and use DataStore in Android Jetpack package (Part 2)

Tram Ho

Introduce

In Part 1 we looked at and made an example of using Preferences DataStore to read and write a UUID string. Following this section, we will learn about how to use Proto DataStore to read and write a token string. Writing data from Proto DataStore takes more steps and requires pre-defining a schema file in .proto format in the protocol buffer language. If anyone is not familiar with this language, you can find out through the link here https://developers.google.com/protocol-buffers/docs/overview

Use Proto DataStore to store data

Defines the Schema file used for storing information

As introduced in the previous section, Proto DataStore is a way to ensure that the information of the data type is always correct during read and write operations. This guarantee is done by pre-defining a schema file and the program when compiling will read information from the previously defined schema file and generate read and write functions according to the specified data type. that it helps the data to be manipulated correctly.

This proto file is specified to be stored in the app / src / main / proto directory if your project does not have one, then create the correct path as above. Next we proceed to create a file according to the structure of the protobuf language. Here the example named a file MyProtoDataStore.proto has the following information:

The meaning of this file is to create a class named TestModel with the package defined as “com.example.datastore” . In this class, the first element is a variable named token with the data type string , the number 1 represents the first position, not the default value of the token variable. We can see the compile file when running the program will generate the following form.

Install the library

To use Proto DataStore we need to add the following library to the Gradle file of the project:

  • The first is the plugins of the protobuf library

  • Next are the libraries to use proto datastore in the project

  • Finally is the specification to help convert the schema file defined earlier in app / src / main / proto to the corresponding java file that can be used and called from within the program’s source code.

Create a Proto DataStore object

There are two things to do to create a Proto Datastore object for storing the type of object that we defined from the .proto file earlier:

  • Define a class that will implement Serializer <T> , where T is the data type we defined. This Serializer class will tell the DataStore how to read and write our data we defined. The content looks like the class MyProtoDataStoreSerializer below:

  • Next, use the Context.createDataStore () method to create a DataStore <T> object, where T is the defined data type from the proto file. This function will have 2 parameters: fileName and serializer . Where fileName is the filename used for storing the data here is MyProtoDataStore.proto , while the serializer is the parameter that tells DataStore the name of the serializer class that was defined earlier, which is the class MyProtoDataStoreSerializer . The specific code is as below:

Write data to Proto DataStore

Proto DataStore provides updateData () method to perform an object update operation to DataStore. In this example we want to store a token string, so the logging would be done as follows:

Note like the DataStore Preferences , Proto DataStore also implements asynchronous storage associated with Coroutines, so when declaring a function to store data, there will be more suspend at the top of the function.

Read recorded data from Proto DataStore

Use the DataStore.data method to retrieve data and the data is stored as a Flow . The example here is the token string, the output data will be in the form Flow <String> . The function to read data would look like this:

Similar to part 1, we must also use the collect method to listen to the data emitted from this Flow <String> .

Results when running the program:

At this point, we have finished reading and writing a value using the Proto DataStore storage method.

The source code of the project can be found here: https://github.com/hungan1409/ExampleDataStore.git

Refer

https://developer.android.com/topic/libraries/architecture/datastore

Share the news now

Source : Viblo