如何解决错误“有多个数据存储活动”
我在项目中有错误。当我在项目上使用数据存储时,我会遇到错误。 我不打算为我的项目创建多个数据存储。我确实想为它创建一个数据存储,但是运行项目时,我会遇到一个错误
,我在stackoverflow.com上找到了许多问题,例如
2- 为什么创建多个数据存储?
等等...
但是我的项目错误未解决
如何解决该
这是我的代码 userpreferences.kt
class UserPreferences @Inject constructor(context: Context) {
// private val dataStore = context.dataStore
private val Context.dataStore by preferencesDataStore(name = "userPrefs")
private val context = context.applicationContext
companion object {
private val ACCESS_TOKEN_KEY = stringPreferencesKey("accessToken")
private val REFRESH_TOKEN_KEY = stringPreferencesKey("refreshToken")
}
val getAccessToken: Flow<String>
get() = context.dataStore.data.map {
it[ACCESS_TOKEN_KEY]?:""
}
suspend fun setRefreshToken(value: String) {
context.dataStore.edit { it[ACCESS_TOKEN_KEY] = value }
}
val getRefreshToken: Flow<String>
get() = context.dataStore.data.map {
it[REFRESH_TOKEN_KEY]?:""
}
suspend fun setAccessToken(value: String) {
context.dataStore.edit { it[REFRESH_TOKEN_KEY] = value }
}
suspend fun clear() {
context.dataStore.edit { preferences ->
preferences.clear()
}
}
}
错误:
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.alpha.caller, PID: 11412
java.lang.IllegalStateException: There are multiple DataStores active for the same file: /data/user/0/com.example/files/datastore/userPrefs.preferences_pb. You should either maintain your DataStore as a singleton or confirm that there is no two DataStore's active on the same file (by confirming that the scope is cancelled).
at androidx.datastore.core.SingleProcessDataStore$file$2.invoke(SingleProcessDataStore.kt:168)
at androidx.datastore.core.SingleProcessDataStore$file$2.invoke(SingleProcessDataStore.kt:163)
at kotlin.SynchronizedLazyImpl.getValue(LazyJVM.kt:74)
at androidx.datastore.core.SingleProcessDataStore.getFile(SingleProcessDataStore.kt:163)
at androidx.datastore.core.SingleProcessDataStore.readData(SingleProcessDataStore.kt:380)
at androidx.datastore.core.SingleProcessDataStore.readDataOrHandleCorruption(SingleProcessDataStore.kt:359)
at androidx.datastore.core.SingleProcessDataStore.readAndInit(SingleProcessDataStore.kt:322)
at androidx.datastore.core.SingleProcessDataStore.readAndInitOrPropagateAndThrowFailure(SingleProcessDataStore.kt:302)
at androidx.datastore.core.SingleProcessDataStore.handleUpdate(SingleProcessDataStore.kt:281)
at androidx.datastore.core.SingleProcessDataStore.access$handleUpdate(SingleProcessDataStore.kt:76)
at androidx.datastore.core.SingleProcessDataStore$actor$3.invokeSuspend(SingleProcessDataStore.kt:242)
at androidx.datastore.core.SingleProcessDataStore$actor$3.invoke(Unknown Source:8)
at androidx.datastore.core.SingleProcessDataStore$actor$3.invoke(Unknown Source:4)
at androidx.datastore.core.SimpleActor$offer$2.invokeSuspend(SimpleActor.kt:122)
at kotlin.coroutines.jvm.internal.BaseContinuationImpl.resumeWith(ContinuationImpl.kt:33)
at kotlinx.coroutines.DispatchedTask.run(DispatchedTask.kt:106)
at kotlinx.coroutines.scheduling.CoroutineScheduler.runSafely(CoroutineScheduler.kt:571)
at kotlinx.coroutines.scheduling.CoroutineScheduler$Worker.executeTask(CoroutineScheduler.kt:750)
at kotlinx.coroutines.scheduling.CoroutineScheduler$Worker.runWorker(CoroutineScheduler.kt:678)
at kotlinx.coroutines.scheduling.CoroutineScheduler$Worker.run(CoroutineScheduler.kt:665)
I have an error in the project. When I use the DataStore on my project then I get an error.
I do not intend to create several datastore for my project. I do want to create a datastore for it but when running the project I get an error
and I found many questions on the StackOverflow.com like
1- Git push results in "Authentication Failed"
2- Why there is multiple Datastore is created?
and etc...
but my project error's not resolved
how to fix that
this is my code in the UserPreferences.kt
class UserPreferences @Inject constructor(context: Context) {
// private val dataStore = context.dataStore
private val Context.dataStore by preferencesDataStore(name = "userPrefs")
private val context = context.applicationContext
companion object {
private val ACCESS_TOKEN_KEY = stringPreferencesKey("accessToken")
private val REFRESH_TOKEN_KEY = stringPreferencesKey("refreshToken")
}
val getAccessToken: Flow<String>
get() = context.dataStore.data.map {
it[ACCESS_TOKEN_KEY]?:""
}
suspend fun setRefreshToken(value: String) {
context.dataStore.edit { it[ACCESS_TOKEN_KEY] = value }
}
val getRefreshToken: Flow<String>
get() = context.dataStore.data.map {
it[REFRESH_TOKEN_KEY]?:""
}
suspend fun setAccessToken(value: String) {
context.dataStore.edit { it[REFRESH_TOKEN_KEY] = value }
}
suspend fun clear() {
context.dataStore.edit { preferences ->
preferences.clear()
}
}
}
Error:
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.alpha.caller, PID: 11412
java.lang.IllegalStateException: There are multiple DataStores active for the same file: /data/user/0/com.example/files/datastore/userPrefs.preferences_pb. You should either maintain your DataStore as a singleton or confirm that there is no two DataStore's active on the same file (by confirming that the scope is cancelled).
at androidx.datastore.core.SingleProcessDataStore$file$2.invoke(SingleProcessDataStore.kt:168)
at androidx.datastore.core.SingleProcessDataStore$file$2.invoke(SingleProcessDataStore.kt:163)
at kotlin.SynchronizedLazyImpl.getValue(LazyJVM.kt:74)
at androidx.datastore.core.SingleProcessDataStore.getFile(SingleProcessDataStore.kt:163)
at androidx.datastore.core.SingleProcessDataStore.readData(SingleProcessDataStore.kt:380)
at androidx.datastore.core.SingleProcessDataStore.readDataOrHandleCorruption(SingleProcessDataStore.kt:359)
at androidx.datastore.core.SingleProcessDataStore.readAndInit(SingleProcessDataStore.kt:322)
at androidx.datastore.core.SingleProcessDataStore.readAndInitOrPropagateAndThrowFailure(SingleProcessDataStore.kt:302)
at androidx.datastore.core.SingleProcessDataStore.handleUpdate(SingleProcessDataStore.kt:281)
at androidx.datastore.core.SingleProcessDataStore.access$handleUpdate(SingleProcessDataStore.kt:76)
at androidx.datastore.core.SingleProcessDataStore$actor$3.invokeSuspend(SingleProcessDataStore.kt:242)
at androidx.datastore.core.SingleProcessDataStore$actor$3.invoke(Unknown Source:8)
at androidx.datastore.core.SingleProcessDataStore$actor$3.invoke(Unknown Source:4)
at androidx.datastore.core.SimpleActor$offer$2.invokeSuspend(SimpleActor.kt:122)
at kotlin.coroutines.jvm.internal.BaseContinuationImpl.resumeWith(ContinuationImpl.kt:33)
at kotlinx.coroutines.DispatchedTask.run(DispatchedTask.kt:106)
at kotlinx.coroutines.scheduling.CoroutineScheduler.runSafely(CoroutineScheduler.kt:571)
at kotlinx.coroutines.scheduling.CoroutineScheduler$Worker.executeTask(CoroutineScheduler.kt:750)
at kotlinx.coroutines.scheduling.CoroutineScheduler$Worker.runWorker(CoroutineScheduler.kt:678)
at kotlinx.coroutines.scheduling.CoroutineScheduler$Worker.run(CoroutineScheduler.kt:665)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这也使我丢了,但是我弄清楚了(又名,猜测为直到起作用):
这是A
,但是如果您需要自定义序列化器,则可以执行以下操作(与旧方法相同的参数):
This was throwing me too, but I figured it out (aka, guessed until it worked):
This is for a
, but if you need a custom serializer, you can do the following (same parameters as the old method):