androidx.datastore
Top-level functions summary
ReadOnlyProperty<Context, DataStore<T>> |
<T : Any?> dataStore(Creates a property delegate for a single process DataStore. |
ReadOnlyProperty<Context, DataStore<T>> |
@RequiresApi(value = 24)Creates a property delegate for a single process DataStore to be used in direct boot. |
Extension functions summary
File |
Context.dataStoreFile(fileName: String)Generate the File object for DataStore based on the provided context and name. |
Top-level functions
dataStore
fun <T : Any?> dataStore(
fileName: String,
serializer: Serializer<T>,
corruptionHandler: ReplaceFileCorruptionHandler<T>? = null,
produceMigrations: (Context) -> List<DataMigration<T>> = { listOf() },
scope: CoroutineScope = CoroutineScope(Dispatchers.IO + SupervisorJob())
): ReadOnlyProperty<Context, DataStore<T>>
Creates a property delegate for a single process DataStore. This should only be called once in a file (at the top level), and all usages of the DataStore should use a reference the same Instance. The receiver type for the property delegate must be an instance of Context.
This should only be used from a single application in a single classloader in a single process.
Example usage:
val Context.myDataStore by dataStore("filename", serializer)
class SomeClass(val context: Context) {
suspend fun update() = context.myDataStore.updateData {...}
}
| Parameters | |
|---|---|
fileName: String |
the filename relative to Context.applicationContext.filesDir that DataStore acts on. The File is obtained from |
serializer: Serializer<T> |
The serializer for |
corruptionHandler: ReplaceFileCorruptionHandler<T>? = null |
The corruptionHandler is invoked if DataStore encounters a |
produceMigrations: (Context) -> List<DataMigration<T>> = { listOf() } |
produce the migrations. The ApplicationContext is passed in to these callbacks as a parameter. DataMigrations are run before any access to data can occur. Each producer and migration may be run more than once whether or not it already succeeded (potentially because another migration failed or a write to disk failed.) |
scope: CoroutineScope = CoroutineScope(Dispatchers.IO + SupervisorJob()) |
The scope in which IO operations and transform functions will execute. |
| Returns | |
|---|---|
ReadOnlyProperty<Context, DataStore<T>> |
a property delegate that manages a datastore as a singleton. |
deviceProtectedDataStore
@RequiresApi(value = 24)
fun <T : Any?> deviceProtectedDataStore(
fileName: String,
serializer: Serializer<T>,
corruptionHandler: ReplaceFileCorruptionHandler<T>? = null,
produceMigrations: (Context) -> List<DataMigration<T>> = { listOf() },
scope: CoroutineScope = CoroutineScope(Dispatchers.IO + SupervisorJob())
): ReadOnlyProperty<Context, DataStore<T>>
Creates a property delegate for a single process DataStore to be used in direct boot.
| Parameters | |
|---|---|
fileName: String |
the filename relative to Context.applicationContext.filesDir that DataStore acts on. The File is obtained from |
serializer: Serializer<T> |
The serializer for |
corruptionHandler: ReplaceFileCorruptionHandler<T>? = null |
The corruptionHandler is invoked if DataStore encounters a |
produceMigrations: (Context) -> List<DataMigration<T>> = { listOf() } |
produce the migrations. The ApplicationContext is passed in to these callbacks as a parameter. DataMigrations are run before any access to data can occur. Each producer and migration may be run more than once whether or not it already succeeded (potentially because another migration failed or a write to disk failed.) |
scope: CoroutineScope = CoroutineScope(Dispatchers.IO + SupervisorJob()) |
The scope in which IO operations and transform functions will execute. |
| Returns | |
|---|---|
ReadOnlyProperty<Context, DataStore<T>> |
a property delegate that manages a datastore as a singleton. |
Extension functions
dataStoreFile
fun Context.dataStoreFile(fileName: String): File
Generate the File object for DataStore based on the provided context and name. The file is generated by calling File(context.applicationContext.filesDir, "datastore/$fileName"). This is public to allow for testing and backwards compatibility (e.g. if moving from the dataStore delegate or context.createDataStore to DataStoreFactory).
Do NOT use the file outside of DataStore.
The context of the application used to get the files directory @fileName the file name