Read More
Read now

Proto DataStore in Android

Save

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

Setup

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.

. . .

Leave a Comment

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


Be the first to comment.

css.php

Big thanks to Webkul and his team for helping get Opencart 3.0.3.7 release ready!

Deniel Kerr

Owner and Founder. Opencart

Talk to Sales
Start a Project

    Message Sent!

    If you have more details or questions, you can reply to the received confirmation email.

    Back to Home