Android Jetpack DataStore

How to store data asynchronously in Jetpack Compose?

Mansi Shah
Jetpack Composers
Published in
3 min readOct 7, 2022

--

Introduction

DataStore provides a safe and durable way to store small amounts of data, such as preferences and application states. It uses Kotlin coroutines and Flows to store data asynchronously.
It does not support partial updates: if any field is modified, the whole object will be serialized and persisted to disk. It is thread-safe and non-blocking.

Why DataStore?
In particular, it addresses these design shortcomings of the SharedPreferences API:

  • Synchronous API encourages StrictMode violations
  • apply() and commit() have no mechanism for signaling errors
  • apply() will block the UI thread
  • It can return state that is not yet persisted
  • No consistency or transactional semantics
  • Throws runtime exception on parsing errors
  • Exposes mutable references to its internal state

Types of DataStore

Preferences DataStore: It stores and accesses data using keys. This implementation does not require a predefined schema and does not provide type safety.

Proto DataStore: It stores data as instances of a custom data type. This implementation requires you to define a schema using protocol buffers, but it provides type safety.

From Android developer docs.

Preferences DataStore with Example

In this blog, we will go through the Preferences DataStore example.
Let’s take a simple example of the Login module. When the Merchant successfully logs in to the app, his Email Id will be stored in DataStore.

  • Add the below dependency in the app/build.gradle
  • After adding a dependency, create a DataStoreManager.kt file inside prefdatastore directory under the app’s package name like below.
  • In this file, we will create our DataStore with a proper name. It should only be called once in a file (at the top level), and all usages of the DataStore should use a reference from the same Instance. The receiver type for the property delegate must be an instance of Context.
  • We will declare suspended functions saveToDataStore() to store data & getFromDataStore() to fetch data in this file.
  • In our app, we are storing the Merchant’s Email at the time of Login and Mobile number, Business name, Business address, Website, Location, Business category, etc at the time of Registration. DataStoreManager.kt file will look like the below.
  • We have one more function clearDataStore() to clear Merchant’s data on Logout.
  • Here, we have used a data class MerchantDetail.kt to store the Merchant’s Data.
  • Now in the Login screen inject DataStoreManager class. Save the email to DataStore in a coroutine scope when the merchant clicks on a Login button. Here I have used lifecycle scope.

Done. You have saved your data successfully in DataStore! 😎

That’s a wrap for now! Many more to come 💥
I hope you find this blog helpful.

--

--

Mansi Shah
Jetpack Composers

Lead Android Engineer. Hands-on experience in Java, Kotlin, and Dart. Obsessed with Jetpack Compose! Travel Geek. Painting Lover. https://linktr.ee/mansik15