DataStore / Jetpack

DataStore: Basics

In Early September 2020 Google released the first Alpha of a new Jetpack library named DataStore. DataStore is a replacement for SharedPreferences. While there are some similarities to SharedPreferences, DataStore offers far greater flexibility. In this series of posts we’ll take a detailed look at this new library.

DataStore comes in two separate flavours: Preferences and Proto. The Preferences flavour stores data as simple key / value pairs, and is pretty similar to SharedPreferences in the respect. However, Protobuf is the foundation for the Proto library which allows us to use more complex data structures.

At the time of writing, the official documentation and codelab mainly focus upon the Preferences flavour. Although there is a simple example of the Proto flavour, there are some omissions which mean that we can’t just lift the example code and use it in our projects. Moreover, the strongly-typed nature of the Proto flavour make it far more interesting. Also, the API for the Proto flavour makes it far more flexible. So this series will focus upon the Proto flavour.

Update: Since writing this article a DataStore Proto codelab has been published which configures the build similarly to how I’ve done it here. However, we’ll be building upon this foundation in future articles in this series.

Code Generation

I mentioned some omissions in the official documentation, so let’s qualify that. The Proto documentation shows the proto schema which represents the data structure that will be persisted using Protobuf. However, we must perform code generation to create the JVM components representing this. Specifically this is the Settings class that is used as the generic type for both SettingsSerializer and DataStore in the Kotlin example.

While this may initially feel like a major omission, it is quite understandable if we consider that there are multiple tools available for this. Different projects may have different requirements, so we can chose the tool which best meets those requirements. We are not tied to any specific code generation tool which is a sign of a well designed library.

Google Protobuf Tools

To get things working, I chose the official Google code gen tools. These consist of three components: A Gradle plugin, the Protobuf compiler, and the Protobuf runtime library. Specifically I chose to go with the Java lite option because it has a smaller runtime library making it more suitable for mobile.

First we need to add the Gradle plugin to our top level build.gradle:

Next we need to update the app build.gradle file:

First we apply the Protobuf gradle plugin. Then we add a dependency for the java-lite runtime library. Finally the protobuf config block is the configuration for the Gradle plugin. It specifies which version of the protac compiler to use (I had issues if the compiler version and runtime library version did not match). The generateProtoTasks block adds a command line flag to the protac invocation which specifies that we should use lite mode.

It took me a fair bit of trial and error (and searching StackOverflow!) to get a working configuration. But hopefully this will help those trying DataStore Proto for the first time.

SimpleData

Now that we have the build tools in place, let’s start with a simple example. First we’ll create the proto schema for the data that we want to persist:

This is about as simple as we can go. Don’t worry we’ll explore more complexity later in the series. The first line specifies that we’ll use version 3 of the protobuf syntax. Next we define the package name under which the java classes will be generated.

The next line forces each java class to be generated in a separate file. The protobuf compiler generates three separate classes, and it will cause build issues if these get generated inside a single file.

Finally we have our data structure named SimpleData which contains a single string field.

The generated Java class source code is actually about 280 lines. So the generated code is far more than a simple POJO.

We must write a Serializer persist this data. This is a wrapper around some of the persistence methods that are in the generated class:

This might appear to be unnecessary boilerplate which could easily have been built in to DataStore. However, it is actually an important abstraction point. The value of this will become apparent later in the series.

The DataStore

Now that we have the data itself and the ability to persist it, we can create the DataStore itself. I chose to use Hilt in the sample app, so we have a Dagger Module which is capable of injecting the DataStore wherever it may be required.

The providesDataStore() method is responsible for creating a DataStore instance. This specifies a filename in app storage to which the data will be persisted, and the Serializer to use to persist the data. In this case it’s the Serializer that we just created.

ViewModel

The ViewModel where we’ll use this looks like this:

There’s quite a bit going on here, so let’s break it down. A DataStore<SimpleData> instance is injected. The text file is a LiveData<String> which represents the value of the single String field within SimpleData. The map function maps the SimpleData object to its text field. This is then converted from a Flow to LiveData.

The mutableText field is a wrapper around this the text LiveData, converting it to a MutableLiveData instance using TwoWayBinder:

This take a LiveData object and a lambda as arguments. It observes liveData when it has active observers. Whenever the value of liveData is updated, it checks whether the value has actually change. If it has, it invokes the lambda.

Going back to mutableText in the ViewModel, TwoWayBinder wraps text and, whenever its value changes, the lambda is invoked. The change detection is important here. The value of mutableText can change for one of two reasons: Either the value of text changes because the DataStore value has changed, or a consumer changes the value of mutableData.

In the first case, the change is being emitted from DataStore because the value has changed. In the second case, the lambda will update the value in the DataStore which will get persisted, and will cause DataStore to emit a new value. Without the value changed check in TwoWayBinding this will cause an infinite loop.

I should mention that the updateData() method of DataStore is a suspend function hence we must wrap it in a launch block. DataStore will automatically perform the work on an IO thread so we don’t need to explicitly do that here.

Also, there is no synchronous method for obtaining the current value from DataStore

The Layout

Let’s take a look at the layout now that the business logic is in place. I have opted to use DataBinding here because it play extremely nicely with LiveData. I opted to use two way data binding here, and that is reflected in the naming of the TwoWayBinder class that we saw earlier.

Here we have an EditText and a TextView.

Let’s start with the TextView as it’s is simpler. It is bound to the text field in the ViewModel. Whenever the value of that changes, then the text of the TextView will be updated. A change in the value store in the DataStore will trigger an update to the TextView text.

The EditText has a two-way binding on mutableText – the @= prefix indicates two-way binding. This behaves identically to the TextView, but will also update the value of mutableText whenever the user chances the contents of the field. So editing this will trigger an update to the persisted value in the DataStore.

The net result of all this is that if we alter the contents of the EditText, these changes get propagated through to DataStore which persists the changes, and then triggers an update on the Flow. This updates the text in the TextView:

Synchronous Operation

One important thing to remember about DataStore is that it does not provide a synchronous mechanism for reading persisted values. The only option is to subscribe to a Flow. There might be a temptation to wrap a call in runBlocking {} and return the first value emitted on the Flow. However, I would try and avoid this. It will perform potentially expensive operations on the UI thread. A far better approach is to subscribe to the Flow for the lifecycle of the component within which it will be accessed. Then that component can access the last emitted value to get the current state.

For example, in a ViewModel we can do something like this:

This will work for the vast majority of case – we can simple use current to obtain the latest state. The only potential issue is the race condition if we attempt to access current before an initial value has been emitted on the Flow. We can use ::current.isInitialized to check for that, and observe liveData until it emits a value.

This provides what appears to be synchronous access to the data, without accessing the DataStore synchronously.

Conclusion

We have a basic example working. However there’s an awful lot more that we can build on top of this foundation. In the next article we’ll consider alternatives to using the Google Protobuf codegen.

The source code for this article is available here.

© 2020, Mark Allison. All rights reserved.

Copyright © 2020 Styling Android. All Rights Reserved.
Information about how to reuse or republish this work may be available at http://blog.stylingandroid.com/license-information.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.