Redeem Now
Read More
Read now

Android Jetpack DataStore

Jetpack DataStore is a new data storage solution and an alternative improvement upon Shared Preferences.

Jetpack DataStore library uses Kotlin coroutines and Flow for easier asynchronous reads and writes.

This library allows you to store key-value pairs or typed objects with protocol buffers.

Protocol buffers are Google’s extensible mechanism for serializing structured data.

Types of DataStore

There are  different approaches in DataStore :

SharedPreferences DataStore

Preferences DataStore

Proto DataStore

If you are interested to use Jetpack DataStore for storage in your application then this blog will help you to achieve your goal.

Initial setup

First, start with adding DataStore dependency in your Gradle file

if you want to use Proto DataStore, don’t forget to add the below dependency :

Using DataStore

if you are using proto DataStore schema is defined in the proto file on app/src/main/proto/ directory.

Create a DataStore

Create a Preferences DataStore

Context.createDataStore() extension function is used to create an instance of Preferences DataStore with a mandatory name parameter which is the name of the Preferences DataStore.

Create a Proto DataStore

Here two parameters fileName is the name of the file on app/src/main/proto/  which stores the data and serializer is the name of the serializer class which is CustomerSerializer in my case.

Read DataStore

Preferences DataStore Read

preferencesKey() is used with key for each value that needs to be store in DataStore.

Proto DataStore Read

Write DataStore

Preferences DataStore Write

edit() function is to updates the data in a DataStore.

Proto DataStore Write

For updating the stored data object use updateData() function.

. . .

Comment

Add Your Comment

Be the first to comment.

css.php
Hire Us!
Brief us about your requirements and we'll get back to you.
Woo! Hooy!
We have just recieved your project brief and our expert will contact you shortly.
Send Again
Close