Redeem Now
Read More
Read now

Proto DataStore in Android


Proto Datastore allows us to store custom type data. It uses protocol buffers to store data.

In share preference, we can only save data in key-value. DataStore allows us to store data in key-value pair and in form of object as well.

Proto DataStore is one way of DataStore to save data.

Please check my previous blog for datastore and preferences datastore : Preferences DataStore


Add Dependency

Define a schema

We have to create a predefined schema in our project. This schema defines the type of object which we wanna store in Proto DataStore.

Suppose, we wanna save user data. We need to create a .proto file in our project. Lets say User.proto. Directory : app/src/main/proto/

Schema :

NOTE: After creating User.proto file, please rebuild your android project.

Create DataStore

Now, define a class which implements Serializer<T> , where T is the type defined in the proto file. This class will define how to read or write data.

Use createDataStore() function for creating instance of DataStore.

Read Data from DataStore

Get user id :

Get user name :

Write Data in DataStore

updateData method is used to save data in Proto DataStore.

To learn more about datastore, check following link : DataStore

Hopefully, this blog will be helpful for you.

. . .


Add Your Comment

Be the first to comment.

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