Wild Storage | Part 3— DataStore and SharedPreferences
For Android Developers
Easy data layer for every Application, here is the list of the blogs in this series:
- Part 1 — Performance for queries
- Part 2 — Threading and Concurrency
- Part 3 — DataStore and SharedPreferences
This is the second part of the series “Wild Storage for Android developers” today we are gonna discuss Data Store and Shared preferences, specifically why they are an alternative to the normal Database
So in the past part, we talk about how Room can help us to handle threading and concurrency, this is something that was missing inside SharedPreferences which is one of the most primitive ways to save data inside Android! Let’s talk about Shared preferences and Data Store
Shared Preferences and Data Store
I have been getting this question around for a while, DataStore is the same as Shared Preferences? Well, the short answer is no!
The main reason is that SharedPreferences has the next qualities that make it one of the most simple approaches, also pretty much follows the pattern of a file that stores a Key-Value in an XML file, that you can found in a debug app if you search for it, whatsoever is private for other apps and even trying we can not reach for it, a couple of years ago you can create a SharedPreference file with some public modes like MODE_WORLD_READABLE
and MODE_WORLD_WRITEABLE
that have been deprecated and if you wanna use something like this, your app will crash with a SecurityException.
- Synchronous updates — All the process run in the main thread
- Primitive Values — The only values available are Boolean, Int, String, or even a Set<T> where T is a primitive value
- They can also be encrypted — SharedPreferences are the main instrument inside the incredible library
androidx.security:security-crypto
that can encrypt data using a KeyStore approach, but it also follows the same restrictions and approaches that normal SharedPreferences, so, you can only encrypt primitive values, that is more than enough for multiple tasks that requires encryption.
If you wanna know how to encrypt data, you can review my past serial “Modern Security for Android Developers” on Google Developer Expert Blog
For DataStore is focus on handling the overwork some teams were putting on SharedPreferences, especially with a focus on the threading integration.
- Asynchronous updates — All the process of editing and fetching data from the schema of DataStore are addressed over a flow that represents the stream of values that are being asynchronously computed
- Objects — You can have an object inside DataStore with the use of ProtoBuffers that will make this approach type safe, without this, you will be able to store only Key-Value primitives without type safety
- Encryption — There’s non available encryption for the moment for DataStore, but you can Encrypt a full data base using Room or Realm
One of the most important parts of DataStore in a simple way:
Using the first way, you will create a simpler DataStore<Preferences> object and with the secon, you will create a migration from the SharedPreferences you were using in the past.
DataStore use Flow to extract data from your Preferences object, that provides efficient, cached access to the latest persisted state, then flow will take one of this two actions: Emit a value or throw an exception
You can safe data and get the values using this approach
For Protobuffers, we need to create an new object that extends Serializer with all override all the objects need it to a sucessful serialization.
This is all for this serial! Feel free to ask any question here or at my Twitter account that you can found below!
If you need help, I’m always happy to help, you can find me here:
Medium as Dinorah Tovar
Twitter as @ddinorahtovar
StackOverflow as Dinorah Tovar
Happy Coding! 👩🏻💻