Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the best alternative for Shared Preferences In Android?

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?

like image 387
Mr.India Avatar asked Jan 31 '16 19:01

Mr.India


People also ask

Why we use shared preferences in Android?

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.

Where shared preferences are stored in Android device?

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.

What is difference between preferences and SharedPreferences?

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.

Can an app have multiple shared pref files?

Yes you can maintain as many shared preference files for an app as you can.


2 Answers

It's very hard to recommend you anything without a deep understanding of your usecase.

  • If you want to store some user-preference data - SharedPreferences may be a good choice.
  • If you want to store authentication data, like your users' auth-tokens - don't use SharedPreferences and take a look at AccountManager instead.
  • If you want to store business data e.g. multiple business entities, that keep some relations to each other, you want to be able to query on them and/or modify it - I'd recommend you to use Realm - https://realm.io. Alternative is to use SQLite but in my very subjective opinion Realm is much easier to start with.
  • If you just want to cache some JSON-based responses - take a look at caching mechanisms that your HTTP client may offer you. 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.

like image 104
Mateusz Herych Avatar answered Oct 19 '22 12:10

Mateusz Herych


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

like image 10
Velmurugan Murugesan Avatar answered Oct 19 '22 12:10

Velmurugan Murugesan