Android: Benefits of DataStore over SharedPreferences

Viewed 8420

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).

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.

Related