opensrp / fhircore

FHIR Core / OpenSRP 2 is a Kotlin application for delivering offline-capable, mobile-first healthcare project implementations from local community to national and international scale using FHIR and WHO Smart Guidelines on Android.
https://opensrp.io
Apache License 2.0
56 stars 56 forks source link

[Quest] Usage of Datastore vs SharedPreference #1835

Closed f-odhiambo closed 1 year ago

f-odhiambo commented 1 year ago

Describe the issue to be researched

Describe the goal of the research

Describe the methodology

GibsonRuitiari commented 1 year ago

If I may, both of them are used to store simple values, such as user preferences or tokens, in android. Datastore [DS] remedies most of the defects suffered by Shared Preference, this is in terms of DS performing its functions asynchronously hence making UI Thread Safe as compared to Shared Pref. Also, DS can store multiple type of values including objects and Proto buffs whereas Shared Pref only allows primitive values.

The one defect DS suffers from is encryption. Shared Pref uses the android sandbox by default to encrypt its values whereas DS does not do that, so values saved are always in a readable format, hence making it unsuitable for storage of sensitive data. See [Encryption issue] (https://issuetracker.google.com/issues/199770477) [as of now you may be forced to write your own encryption, AES encryption preferably, to secure your sensitive data]. Beyond usage of DS, I personally haven't tested the performance of DS, but I have with Shared Preferences, and its unfortunately, slow especially when it comes to persisting [writing] values to disk.

Side note: If you want the best of both worlds: performance, concurrency and security, then you might want to check out MMKV library from Tencent.

ahsanbhatti49 commented 1 year ago

SharedPreferences is the common way used for storing key-value pairs in local storage. Here are some Pros of using Shared prefs.

Disadvantages of using SharedPreferences

Lets talk about the pros of using Datastore :

We can refer this chart : Screenshot from 2022-11-28 17-41-39

References: https://proandroiddev.com/is-jetpack-datastore-a-replacement-for-sharedpreferences-efe92d02fcb3 https://medium.com/knowing-android/wild-storage-part-3-datastore-and-sharedpreferences-b32e8686f22b https://proandroiddev.com/securing-androids-datastore-ad56958ca6ee

f-odhiambo commented 1 year ago

cc @ellykits