Stop using SharedPreferences

Stop using SharedPreferences

In this article I will discuss about why we should stop using SharedPreferences to store small non-relational data. I will also suggest what to use with its small tutorial.

Why we should stop using SharedPreferences? 🤔

You would be thinking as a new android developer that what's wrong with it, its so easy to use and works fine for storing small data sets. Let's deep dive into it and discover some major problems with SharedPrefs.

1- Synchronous - It's always a bad idea to do I/O operations on UI Thread. Sharedpreferences uses main/UI thread to perform its tasks.

2- Not Durable in some cases - let's just suppose that user updates any value which we store on SharedPrefs, it will reflect to the memory before it's successfully persisted over the disk. So, sometimes on restarting the application, users might see older data instead of getting the updated one's.

3- OEM bugs - SharedPrefs has poorly defined behaviour for writes, which makes SharedPref no available for many devices.

*Note - There may be many other points too but these are some which I have discovered.

What's the alternative?

1- SQLite

2- LevelDB

... and many more

But the problem is, to store such a small amount of data, non of the developers will be willing to implement a whole lot database to store the required data's. These Local DB's are complex to implement.

Around Sept, 2020, google announced a new tool which is under the Jetpack Components, named DataStore which works completely fine out of the box. It's actually the replacement of SharedPrefererence and improves the loop holes of later.

DataStorage and it's advantages

DataStore Part of Android Jetpack. It's a data storage solution that allows you to store key-value pairs or typed objects with protocol buffers.

Some of it's major advantage over SharedPrefs are:

  • It uses Kotlin coroutines internally which makes its whole process Asynchronous.
  • It uses Kotlin Flows internally which makes it easy to make I/O operations.
  • Easy Data migrations
  • Data Consistency is maintained (All transactions are maintained well)
  • Data corruptions are handled properly

How to use DataStore API

Just follow the code and comments beside them, its super easy stuff to use.

//declaration of variable

private val store: DataStore<Preferences> by preferencesDataStore("nameOfTheStorage")

//saving data, we have to use suspend function as it internally uses coroutines

    private suspend fun save(key: String, value: String) {
        val dataKey = stringPreferencesKey(key)
        dataStorage.edit { settings ->
            settings[dataKey] = value
        }
    }

//reading the data which will again be used inside a suspend function

 private suspend fun read(key: String): String? {
        val dataKey = stringPreferencesKey(key)
        val preferences = dataStorage.data.first()
        return preferences[dataKey]
    }

Wohooo🚀, thats all we have to do with DataStore and it will do its job.

Hope you have learnt something new. Thanks for reading.

Keep learning! 🔥

Did you find this article valuable?

Support Subhadip Das by becoming a sponsor. Any amount is appreciated!

Â