如何解决如何在多用户或文件中使用 Android DataStore
我想使用 DataStore 存储一些首选项。但问题是我的应用程序可以有多个用户,因此需要将这些首选项存储在单独的文件中。我得到了一个仅使用一个用户的工作示例,但我很难支持多个用户。
这是我的代码示例:
class DataStorageRepository(private val context: Context,private val userRepository: UserRepository) {
private object PreferencesKeys {
val SETTING_ONE = intPreferencesKey("setting_one")
}
// retrieve datastore for currently logged in user.
private val Context.dataStore: DataStore<Preferences> by preferencesDataStore(name = userRepository.currentRegistration().name)
val userPreferencesFlow: Flow<UserPreferences> = context.dataStore.data.map { preferences ->
val settingOne = preferences[PreferencesKeys.SETTING_ONE] ?: 0
UserPreferences(settingOne)
}
suspend fun storeSettingOne(settingOne: Int) {
context.dataStore.edit { preferences ->
preferences[PreferencesKeys.SETTING_ONE] = settingOne
}
}
data class UserPreferences(val lastUsedToAccountTab: Int)
}
我正在使用 Koin
并尝试在注销时卸载 DataStorageRepository
并在登录时重新创建它,但 DataStore 似乎一直处于活动状态,直到应用程序被杀死并且出现以下崩溃:
java.lang.IllegalStateException:有多个数据存储处于活动状态 对于同一个文件:[...] 您应该将 DataStore 维护为 单例或确认没有两个 DataStore 处于活动状态 相同的文件(通过确认范围被取消)。
我还尝试使用 Coroutinescope
并在我注销时终止它,但在登录时重新创建范围后,DataStore 似乎没有重新创建。
解决方法
您可以为不同的用户使用不同的密钥或手动保持 DataStore 单一。
例外:
java.lang.IllegalStateException:同一个文件有多个活动的 DataStore:[...] 您应该将您的 DataStore 作为一个单例来维护,或者确认在同一个文件中没有两个活动的 DataStore(通过确认范围被取消)。
androidx.datastore:datastore-*:1.0.0-alpha07
已发布。
将其放在 kotlin 文件的顶层,这样就只有一个实例。
private val Context.dataStore by preferencesDataStore("settings")
class Xxx{
}
https://developer.android.com/jetpack/androidx/releases/datastore#1.0.0-alpha07。
Context.createDataStore 扩展函数已被移除并替换为 globalDataStore 属性委托。在 kotlin 文件的顶层调用 globalDataStore 一次。例如:
val Context.myDataStore by dataStore(...)
把它放在你的 kotlin 文件的顶层,这样它就只有一个实例。 (I57215,b/173726702)
,在我发布此问题的那一刻,我找到了解决此问题的方法。为了解决我的问题,我需要结合我之前的两个解决方案。所以在注销时我卸载 DataStorageRepository
并在登录时重新加载它。我还需要创建一个在注销时取消的 CoroutineScope
。
我的模块
val loggedInModule = module {
single { DataStorageRepository(get(),get()) }
}
我创建了一个范围并将其传递给 DataStore
var loggedInScope: CoroutineScope = CoroutineScope(Dispatchers.Default)
private val Context.dataStore: DataStore<Preferences> by preferencesDataStore(name = userRepository.currentRegistration().name,scope = loggedInScope)
登录时
loggedInScope = CoroutineScope(Dispatchers.Default)
loadKoinModules(loggedInModule)
退出时
loggedInScope.cancel()
unloadKoinModules(loggedInModule)
,
将此行放在伴生对象 { }
private val Context.dataStore: DataStore<Preferences> by preferencesDataStore(name = "settingPrefs")
我的代码
class SettingPrefs(private val context: Context) {
companion object {
private val Context.dataStore: DataStore<Preferences> by preferencesDataStore(name = "settingPrefs")
private val soundKey = booleanPreferencesKey("sound")
private val vibrateKey = booleanPreferencesKey("vibrate")
}
val getSound: Flow<Boolean>
get() = context.dataStore.data.map {
it[soundKey] ?: true
}
suspend fun setSound(value: Boolean) {
context.dataStore.edit { it[soundKey] = value }
}
val getVibration: Flow<Boolean>
get() = context.dataStore.data.map {
it[vibrateKey] ?: true
}
suspend fun setVibration(value: Boolean) {
context.dataStore.edit { it[vibrateKey] = value }
}
}
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。