Which is the best alternative for Shared Preferences
in android to store data, If I want to Read the data and again save it with some changes.
Data may be a user's profile, a json response or any object.
As I store a lot of data, I am searching other less time consuming option to Reda/Write data.
Currently my app taking x milliseconds to go from Activity A to Activity B.
Can I reduce this time?
Android provides many ways of storing data of an application. One of this way is called Shared Preferences. Shared Preferences allow you to save and retrieve data in the form of key,value pair.
Android Shared Preferences Overview Android stores Shared Preferences settings as XML file in shared_prefs folder under DATA/data/{application package} directory. The DATA folder can be obtained by calling Environment.
Preferences: The user interfaces part of the settings. It contains different classes which allow one to compose Settings screens from code or XML. Shared Preferences: These are used to store values in XML files. These files are created, maintained and deleted by Android for you.
Yes you can maintain as many shared preference files for an app as you can.
It's very hard to recommend you anything without a deep understanding of your usecase.
SharedPreferences
may be a good choice.SharedPreferences
and take a look at AccountManager
instead.OkHttp
for instance has a pretty good support for that.Speaking of loading time - SharedPreferences
is pretty fast in general, but it really depends on how you use it. If you store big JSON structs in it, then read it fully just to find some specific object based on id - obviously it'll take more time than using a real database for that.
Have in mind that all solutions I proposed (AccountManager
, SharedPreferences
and SQLite/Realm) can perfectly work with each other in one app. Just make sure to choose the proper tool for a given problem.
Jetpack DataStore is a new and improved data storage solution aimed at replacing SharedPreferences. Built on Kotlin coroutines and Flow, DataStore provides two different implementations:
Data is stored asynchronously, consistently, and transactionally, overcoming most of the drawbacks of SharedPreferences.
If you’re currently using SharedPreferences to store data, consider migrating to DataStore instead.
Type Of DataStore
DataStore provides two different implementations:
Preferences DataStore – stores and accesses data using keys. This implementation does not require a predefined schema, and it does not provide type safety.
Proto DataStore – stores data as instances of a custom data type. This implementation requires you to define a schema using protocol buffers, but it provides type safety.
SharedPreferences vs DataStore
SharedPreference blocked the UI thread on pending fsync() calls scheduled by apply(), often becoming a source of ANRs.
SharedPreferences throws parsing errors as runtime exceptions.
In both implementations, DataStore saves the preferences in a file and performs all data operations on Dispatchers.IO thread.
Reference : DataStore – Jetpack Alternative For SharedPreference
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With