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

Tram Ho


In this article we will learn about DataStore, one of the new data storage solutions in the Android Jetpack package and will gradually replace the existing SharePreferences data type.

The current DataStore in this article has just been introduced and is in the alpha stage, so there are still many changes in the future.

DataStore allows storing data in 2 forms: using key-value pairs or self-defined object types with protocol buffer declaration .

DataStore uses a combination of Kotlin Coroutines and Flow to store and read data asynchronously.

Depending on the purpose of data storage, DataStore will also provide different installation methods: Preferences DataStore and Proto DataStore

  • DataStore Preferences : store and retrieve data by keys. This implementation method does not require predefined schemas and it does not guarantee the correctness of the data type to be passed, which must be determined by the programmer.
  • Proto DataStore : store Primitive data or self-defined data. This implementation method requires defining a schema in advance using protocal buffers , but it ensures the correctness of the data.

Comparison between SharedPreferences and DataStore

Below is a detailed statistics table between the traditional storage of SharedPreferences and the two new ways to store data. Preferences DataStore and Proto DataStore

We can see the biggest difference that SharedPreferences uses synchronous storage mechanism, while DataStore is based on asynchronous mechanism and does not occupy the UI thread, so it is safe to call without causing affects the main process of the application.

Use the DataStore Preferences to store data

In this article we will try to use Preferences DataStore to read and write a UUID string, displaying a Toast every time we run into the app indicating the value of the UUID string we read from the DataStore Preferences.

Install the library

To use the DataStore Preferences we add the following library to the Gradle file of the project

In addition, because based on the working mechanism of Coroutines and Flow , we also need to add the following library

Install the source code

Create a Preferences DataStore object

Use the Context.createDataStore () method to create an instance of Preferences DataStore. This method takes a parameter name that represents the name of the DataStore Preferences. In the source code, we will declare a global variable dataStore and the constructor will be called in the onCreate () method as follows:

Write data into the DataStore Preferences

Since this is a mechanism to store data in key-value form , we need to create the key along with the type of data we want to store first. In this example, we want to store a string UUID with the data type String, so we will declare a variable with the following structure:

Next, we use the edit () method that the DataStore Preferences provides to write data. Also note that DataStore uses asynchronous storage mechanism in conjunction with Coroutines, so when declaring a function to store data, there will be more suspend at the beginning of the function, as we can see below:

Read recorded data from the DataStore Preferences

Preferences DataStore.data DataStore provides methods to retrieve the data and the data is stored as a Flow. The example here is the string UUID, the output data will be Flow <String> . The function to read data would look like this:

At this point, we just read the data to get from the DataStore and store it in a uuid variable of type Flow <String> , to be able to use this data we need to use the collect method to listen to the emitted data. from this Flow <String> . It can be understood that this data type is a replacement for RxJava with similar mechanism Observable and Observer , the way to receive data can be seen as below:

The uuid.collect method is a suspend function, so it needs to be run from a Coroutine Scope and listen for data changes, each time there is a change it will automatically listen and update the value like LiveData we usually know. . Here, if the uuid has not been created it will generate a random value and save it to the previously declared DataStore Preferences via the saveUUID function, if it is, it will print the Toast in the middle of the uuid value screen. The results can be seen in the 2 pictures below

At this point we have finished reading and writing a value using the Preferences DataStore storage method. In the next part, we will learn to read and write data using Proto DataStore.

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



Share the news now

Source : Viblo