Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android DataStore: Calling Context.createDataStore from Java

Tags:

android

kotlin

I'm trying to implement the new typed DataStore API in Java and I'm having some issues. All the documentation seems to be in Kotlin only and trying to create a new data store is not as straight forward from the Java side it seems.

Calling DataStoreFactoryKt.createDataStore() from Java requires me to provide all the arguments including the ones with default values in the Kotlin implementation. There doesnt seem to be any @JvmOverloads annotation for that function, resulting in my predicament.

fun <T> Context.createDataStore(
    fileName: String,
    serializer: Serializer<T>,
    corruptionHandler: ReplaceFileCorruptionHandler<T>? = null,
    migrations: List<DataMigration<T>> = listOf(),
    scope: CoroutineScope = CoroutineScope(Dispatchers.IO + SupervisorJob())
): DataStore<T> =
    DataStoreFactory.create(
        produceFile = { File(this.filesDir, "datastore/$fileName") },
        serializer = serializer,
        corruptionHandler = corruptionHandler,
        migrations = migrations,
        scope = scope
)

What's the better way around this, if there is any? Or is the Data Store api simple designed to be used with Kotlin only? I have no idea how I would go about providing a CoroutineScope argument from Java.

like image 259
ardevd Avatar asked Nov 19 '20 14:11

ardevd


People also ask

What is datastore in Android?

DataStore is Google’s new and improved solution for persisting simple pieces of data by using either key-value pairs or protocol buffers for storing typed objects. It does so using Kotlin Coroutines and Flow to make all the transactions asynchronous, making all the data storing and fetching operations more performant and safe!

What is datastore in Kotlin?

Built on Kotlin coroutines and Flow, DataStore provides two different implementations: Proto DataStore, which lets you store typed objects (backed by protocol buffers) and Preferences DataStore, which stores key-value pairs. Data is stored asynchronously, consistently, and transactionally, overcoming some of the drawbacks of SharedPreferences.

How do I add Android dependencies to datastore?

There are two implementations of DataStore: Preferences and Proto . Choose one or the other. You can also add Android-free dependencies to either implementation. Add the dependencies for the implementation you need in the build.gradle file for your app or module:

Is it possible to create a new data store from Java?

All the documentation seems to be in Kotlin only and trying to create a new data store is not as straight forward from the Java side it seems. Calling DataStoreFactoryKt.createDataStore () from Java requires me to provide all the arguments including the ones with default values in the Kotlin implementation.


2 Answers

After updating dataStore dependency to '1.0.0-alpha08' as below.

// DataStore
implementation "androidx.datastore:datastore-preferences:1.0.0-alpha08"

You can have preferences implementation as follow:

private val Context.dataStore by preferencesDataStore("app_preferences")

After that if you like create some preference key:

private object Keys {
    val HIDE_VISITED = booleanPreferencesKey("hide_visited")
}

other options can be stringPreferencesKey, intPreferencesKey, etc.

Saving value example:

context.dataStore.edit { prefs -> prefs[Keys.HIDE_VISITED] = hideVisited }

Reading saved value example:

val hideVisited = preferences[Keys.HIDE_VISITED] ?: false
like image 77
Ercan Avatar answered Sep 27 '22 16:09

Ercan


You need to add to your Grade build file the dependency for DataStore preferences:

implementation "androidx.datastore:datastore-preferences:1.0.0-alpha04"

and not the one for Types, that way you will be able to resolve the androidx.datastore.preferences.Context.createDataStore method that you are expecting:

public fun Context.createDataStore(
    name: String,
    corruptionHandler: ReplaceFileCorruptionHandler<Preferences>? = null,
    migrations: List<DataMigration<Preferences>> = listOf(),
    scope: CoroutineScope = CoroutineScope(Dispatchers.IO + SupervisorJob())
): DataStore<Preferences> =
    PreferenceDataStoreFactory.create(
        corruptionHandler = corruptionHandler,
        migrations = migrations,
        scope = scope
    ) {
        File(this.filesDir, "datastore/$name.preferences_pb")
    }
like image 28
Víctor Albertos Avatar answered Sep 27 '22 16:09

Víctor Albertos