Data Storage Flashcards
What are the four types of data storage for persistent app data?
Lightweight variable storage..
Internal and external storage..
Database storage..
Network (cloud) storage..
What is lightweight storage?
Lightweight storage is a type of low-capacity storage used for data such as user preference. We typically do this using SharedPreferences or its more recent counterpart DataStore, storing them as Key-Value pairs.
What is meant by internal/external storage?
Data storage from within the file system itself. For example, data stored in a .txt file in the application directory.
What is meant by database storage?
Data storage using a structured database - either as part of a local file or a locally hosted database server. For example, SQLite or Room are the two primary (local) database options on Android.
What is meant by network storage?
Network storage means cloud storage, or data storage on an web server. It is perfect for data synchronisation or for backing up data.
What scope are SharedPreferences stored in?
SharedPreferences has three scopes - activity-level, application-level and system-level.
We can access each one using different methods - getPreferences, getSharedPreferences and getDefaultSharedPreferences respectively.
How is data primarily stored in SharedPreferences/DataStore?
As a key-value pair <K, V> of primitive data types, such as int, String or float.
Recall each SharedPreferences ‘get’ method.
Preferences refers to activity level.
SharedPreferences refers to application level.
DefaultSharedPreferences refers to system level.
How may we create a SharedPreferences entity?
First, get the SharedPreferences object using Activity.getSharedPreferences.
Then, get its Editor object using pref.edit().
Finally, we can put data into it, then commit.
What are the main problems with SharedPreferences?
It runs on the main thread, so it can block it.
It is not designed for multi-process applications.
There is no transaction support, meaning changes cannot be rolled back.
What is DataStore?
DataStore is a solution to SharedPreferences’ problems using Kotlin’s coroutines and its ‘Flow’. It includes transactional and thread support, alongside it being type safe.
What is the difference between Preferences DataStore and Proto DataStore?
Preferences follows the same style as SharedPreferences.
Proto is an alternate approach enforcing type safety using type objects to allow for more complex data that usually can’t fit into key-value pairings.
How can we add data to the DataStore?
First, get the DataStore object using context.createDataStore. The DS is scoped to whichever context we use.
Then, we can use the .edit object to insert settings that have a key-value pair using dynamic string indexing.
What is a ContentProvider?
A ContentProvider is an application component designed for data storage and sharing between different applications. It sits between activities and the database, and validates/acquires data on their behalf using queries.
What do we need before creating a ContentResolver?
We need to specify permissions to allow other apps to use the ContentProvider. We can do this in the manifest file, by defining a <provider></provider> element. Setting android:exported to ‘true’ allows other apps to be able to use it. The client app must first ask permission before using it.