Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android: Benefits of DataStore over SharedPreferences

The android Jetpack team recently released the DataStore library (still in alpha) as a way of saving simple data using two implementations:

  1. Preference DataStore has similar functions to SharedPreferences and used to store simple key-value pairs..
  2. Proto DataStore is used for storing custom data types and requires creating a schema.

Rather than use DataStore, why don't we use sharedPreferences for simple datatypes and Room for more complex storage.

What are the BENEFITS OF USING DATASTORE IN ANDROID OVER USING SHAREDPREFERNCES (for simple data) + ROOM (for complex data).

like image 292
Oluwasegun Wahaab Avatar asked Sep 26 '20 08:09

Oluwasegun Wahaab


People also ask

Is jetpack DataStore a replacement for SharedPreferences?

DataStore is a new and improved data storage solution aimed at replacing SharedPreferences. Built on Kotlin coroutines and Flow, DataStore provides two different implementations: Proto DataStore, that stores typed objects (backed by protocol buffers) and Preferences DataStore, that stores key-value pairs.

What are the advantages of using shared preferences over save instance state?

Shared preferences allow you to store small amounts of primitive data as key/value pairs in a file on the device.

Is shared preferences deprecated?

Yes, it is deprecated. Use the AndroidX Preference Library for consistent behavior across all devices. For more information on using the AndroidX Preference Library see Settings.

What is Android DataStore?

Dismiss Got it. Jetpack DataStore is a data storage solution that allows you to store key-value pairs or typed objects with protocol buffers. DataStore uses Kotlin coroutines and Flow to store data asynchronously, consistently, and transactionally.


1 Answers

quoting after Florina Mutanescu

SharedPreferences comes with several drawbacks: a synchronous API that can appear safe to call on the UI thread, no mechanism for signaling errors, lack of transactional API, and more. DataStore is a replacement for SharedPreferences that addresses most of these shortcomings. DataStore includes a fully asynchronous API using Kotlin coroutines and Flow, handles data migration, guarantees data consistency, and handles data corruption.

like image 55
Stachu Avatar answered Sep 19 '22 11:09

Stachu