How to keep track of changes to Android datastore if file is deleted while app is running

43 Views Asked by At

I have a Android Datastore set up for accessing a datastore file according to official docs:

Example usage:
val Context.myDataStore by dataStore("filename", serializer),

I would like to know if the file has been deleted in data/data/etc, while the app is running (maybe by a hacker). Right now, it seems that once the file is deleted, the old flow value is retained, accessed via:

context.myDataStore.data

. What are some options- like storing the file in an even more private location, or somehow listening to file read errors?

I have considered re-creating the reference to the datastore file at the point of usage, but apparently this is not a good practice as per below.

Android docs:

public fun <T> dataStore(
    fileName: String,
    serializer: Serializer<T>,
    corruptionHandler: ReplaceFileCorruptionHandler<T>?,
    produceMigrations: (Context) -> List<DataMigration<T>>,
    scope: CoroutineScope
): 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.

class SomeClass(val context: Context) {
   suspend fun update() = context.myDataStore.updateData {...}
}
0

There are 0 best solutions below