Using Android Jetpack Datastore in Jetpack Compose
In this article, we will learn about Jetpack Datastore which is a new data storage solution in Android and how to use it in a Compose Application.
According to Google:
Jetpack is a suite of libraries to help developers follow best practices, reduce boilerplate code, and write code that works consistently across Android versions and devices so that developers can focus on the code they care about.
Jetpack Datastore is one of the awesome libraries that have being introduced into the growing list of Jetpack Libraries and according to them
Jetpack DataStore is a data storage solution that allows you to store key-value pairs or typed objects with protocol buffers. DataStore uses Kotlin coroutines and Flow to store data asynchronously, consistently, and transactionally
And it is a replacement for our dear old SharedPreferences and there’s a great differences between them.
DataStore Vs SharedPreferences
Types of saving data in DataStore
- Preference DataStore, like SharedPreferences, has no way to define a schema or to ensure that keys are accessed with the correct type.
- Proto DataStore lets you define a schema using Protocol buffers. Using Protobufs allows persisting strongly typed data. They are faster, smaller, simpler, and less ambiguous than XML and other similar data formats. While Proto DataStore requires you to learn a new serialization mechanism, we believe that the strongly typed schema advantage brought by Proto DataStore is worth it.
Using DataStore in Our app
Add the required dependencies to your build file:
After importing the right dependencies, we move the next step.
Saving Data in DataStore
Let’s say we want to our user’s email when logging in, in case we need later in the app. We will create a Kotlin class called SaveUserEmail and this class will be used to handle all the saving and retrieving of our data.
- The SaveUserEmail class will require the
Context
object to create ourDataStore
. - We then create our
DataStore
with thepreferencesDataStore
using extension function of theContext
object. We need to set the name of our datastore with the compulsoryname
parameter. - We need to define a key for each
String
value to save with thestringPreferenceesKey()
function. You also need to define the name of the key withname
parameter (this is available for other primitive values too). - We can create a function to save the email in the
DataStore
. TheDataStore
provides aedit()
method to save the value. We can pass the key and value to theMutablePreferences
in the trailing lambda. Note that theedit()
function is asuspend
function ( which needs to run in a Coroutine Scope). - Now you can get the saved preferences from the datastore with
datastore.data
which returns aFlow<Preferences>
and with the.map{}
operator we can can get theFlow<String>
which is our email by passing the right key into the preferences.
Now we head to our composable and do the necessary stuff:
In order to get the saved data well collect it from flow like this:
After running this we show get the desired result.
And the whole code looks like this:
Thank you for reading! If this helped in anyway I'll love a clap from you.