How to use DataStore with version "androidx.datastore:datastore-core:1.0.0-beta01”?

Viewed 241

DataStore and preferencesDataStore are absent in this version(beta01).

2 Answers

To do this, you need to:

plugins {
id("com.google.protobuf")
}

protobuf {
protoc {
    artifact = "com.google.protobuf:protoc:3.17.3"
}
generateProtoTasks {
    all().forEach { task ->
        task.builtins {
            create("java") {
                option("lite")
            }
        }
    }
}

implementation("androidx.datastore:datastore-core:1.0.0-beta01")
implementation("com.google.protobuf:protobuf-javalite:3.11.0")
implementation("androidx.preference:preference-ktx:1.1.1")

and initialize

    val data: DataStore<SomeMessage> = DataStoreFactory.create(
    serializer = SessionSerializer, // your Serializer
    corruptionHandler = null,
    migrations = emptyList(),
    scope = CoroutineScope(Dispatchers.IO + Job())

And continue as before

    data.updateData {
        it.toBuilder().setAddress("address").build()
    }

  data.collect { ChargingSession ->
            ChargingSession.address
    }

Use the below dependency

implementation("androidx.datastore:datastore-preferences:1.0.0-beta01")
Related