gpt4 book ai didi

java - 如何修复错误 "There are multiple DataStores active"

转载 作者:行者123 更新时间:2023-12-02 18:11:03 30 4
gpt4 key购买 nike

我在项目中有一个错误。当我在我的项目中使用 DataStore 时,出现错误。我不打算为我的项目创建多个数据存储。我确实想为它创建一个数据存储,但是在运行项目时出现错误

我在 StackOverflow.com 上发现了很多问题,比如

1- Git push results in "Authentication Failed"

2- Why there is multiple Datastore is created?

等等...

但是我的项目错误没有解决

如何解决这个问题

这是我在 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)

最佳答案

这也让我很困惑,但我想通了(又名,猜测直到成功):


// Note: This is at the top level of the file, outside of any classes.
private val Context.dataStore by preferencesDataStore("user_preferences")

class UserPreferencesManager(context: Context) {
private val dataStore = context.dataStore
// ...
}

这是一个

DataStore<Preferences>

,但如果您需要自定义序列化程序,您可以执行以下操作(与旧方法相同的参数):

// Still top level!
private val Context.dataStore by dataStore(
fileName = "user_preferences",
serializer = MyCustomSerializer,
)

关于java - 如何修复错误 "There are multiple DataStores active",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/72340267/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com