Search code examples
androidandroid-roomandroid-preferencesdatastore

Android Preferences DataStore vs Existing Room Implementation


I’m new to Android development and I’m about to implement simple Preferences for my app. It appears SharedPreferences is a dead end and has lots of warts, so I’m looking at DataStore (non-Proto) vs Room. Since I ALREADY heavily use Room and LiveData (yes, I know Flow is the new hotness) in my app for other things, is there any benefit to using DataStore too? I understand Room is recommended for large or complex data as I’ve already reviewed the following, but I’m hoping a more seasoned developer can further hit this home for me:

https://android-developers.googleblog.com/2020/09/prefer-storing-data-with-jetpack.html

https://proandroiddev.com/lets-explore-jetpack-datastore-in-android-621f3564b57

https://medium.com/better-programming/jetpack-datastore-improved-data-storage-system-adec129b6e48

Thank you.


Solution

  • The official blog post you linked has a section specifically about Room vs DataStore:

    If you have a need for partial updates, referential integrity, or support for large/complex datasets, you should consider using Room instead of DataStore. DataStore is ideal for small , simple datasets and does not support partial updates or referential integrity.

    User preferences almost always fall into the 'small, simple datasets' that can be easily expressed as key/value pairs (or something more complicated if you want to use the Proto DataStore) that do not need the overhead of a table schema, SQL queries, custom parsing, or the 'relational' part of a relational database.