ProAndroidDev

The latest posts from Android Professionals and Google Developer Experts.

Follow publication

Deep Dive Into Preferences Datastore in Android

Kashif Mehmood
ProAndroidDev
Published in
4 min readAug 3, 2021

--

What is Preferences Datastore?

*Update**
I have updated the code with MVVM architecture and made a separate video on it that explains it completely you can check it out at

Google introduces Datastore as a replacement for shared preferences. Datastore is a data storage solution for storing small datasets in key-value pairs just like shared preferences, However, the difference is that Datastore uses kotlin and coroutines and flow to handle reading and writing operations on separate threads. Unlike shared preferences, you can get kotlin objects instead of JSON strings when reading data, which makes it easy to work with datastore as compared to shared preferences.

What are we gonna build:

if you want the code:

Why Datastore?

There are many benefits of using data store over shared preferences but a few that will take your attention are:

  • Shared preferences are synchronous and run on the main thread, while datastore runs on a separate thread which makes it thread-safe
  • Datastore is relatively easy to use as compared to shared preferences and uses a kotlin first approach.
  • Async API for storing and reading the data (Flow)
  • Safe to call from UI thread (Dispatchers.IO underneath)
  • Shared preferences had no mechanism for signalling errors, lack of transactional API, while datastore stores data asynchronously with consistency and transaction support.

DataStore is a replacement for SharedPreferences that addresses most of the shortcomings that shared preferences had.

Another thing to consider when using datastore is that there are two types of Datastore

-> Preferences Datastore
-> Proto Datastore

When to use what?😕
There is a major difference between the two that helps choose which to use for which use case

-> Proto Datastore provides type safety and stores with the object that is passed to it and generates schema based on the object. If you make a change to the schema you have to rebuild the project, only then proto data store will be able to generate the new schema. If you have complex data such as parcel able objects go for Proto datastore.

  • > Preferences Datastore datastore does not provide type safety as it stores data in key-value pairs. If you have simple data that can be stored like this then you should go for preferences datastore. Most of the time we already know what is the type of a certain variable so there is no need for safety here.

However, both of the variants use Coroutines and Flow to store data asynchronously with consistency and transaction support.

How to use Preferences Datastore?

Let's get straight to code:

Step 1: Create a class that will be used to manage Datastore operations

we have passed the context to this class using constructor injection because context is required to do operations on datastore.

Step 2:

the next step is to create a datastore object using a delegate. The thing you need to consider is that as we are using preferences datastore we need to pass preferences to datastore type, then we need to give a name to the datastore.

Step 3:

In this step, we define the keys that are required by us, as the data is saved in key-value pairs we need to define the keys against which we want to store our data. Datastore uses a special type of keys starting with the type of data you want to store against the key, if you want to store an int you will simply use

intPreferencesKey(“name_of_int”)

Step 4:

Moving to the most awaited part, as you can see we are using a suspend function here, datastore operations need to be run inside a coroutine as the whole purpose of the datastore was thread safety and moving the workload from the main thread to a background thread.
Now we use the context that we passed to our class to access datastore and its edit method which allows us to use the keys that we created to store the data against those keys.

Step 4:

Just like the writing process is done in a coroutine getting as well, and as datastore uses flow you can use common functions like filter and map with it as well, reading and writing data in datastore is as simple as that.-

Step 5:

Now inside the MainActivity, you can use it like any other flow.
Now, is the time to get rid of shared preferences.

Happy Coding! ❤

I am open to work, connect with me on LinkedIn:

https://www.linkedin.com/in/kashif-mehmood-km/

Membership

Read member-only stories

Support writers you read most

Earn money for your writing

Listen to audio narrations

Read offline with the Medium app

--

--

Published in ProAndroidDev

The latest posts from Android Professionals and Google Developer Experts.

Responses (5)

Write a response