AndroidDataStoreFlow和LiveData|所有首选项都会接收更新,即使只更新了一个首选项
创始人
2024-10-07 03:00:43
0

在使用DataStore时,我们可以使用Flow和LiveData来观察数据的变化。但是,在更新单个首选项时,所有首选项都会接收更新。这是因为我们使用了DataStore的全局流。

为了避免这个问题,我们可以为每个首选项创建单独的流。这样,在更新单个首选项时,只有该首选项的观察者会接收到更新。以下是示例代码:

首先,创建一个名为UserPreferences的类,该类包含为每个首选项定义单独流的方法:

class UserPreferences private constructor(context: Context) {

    private val dataStore = context.createDataStore(name = "user_preferences")

    val favoriteColorFlow: Flow
        get() = dataStore.data.catch { exception ->
            if (exception is IOException) {
                emit(emptyPreferences())
            } else {
                throw exception
            }
        }.map { preferences ->
            preferences[KEY_FAVORITE_COLOR]
        }

    val isNightModeFlow: Flow
        get() = dataStore.data.catch { exception ->
            if (exception is IOException) {
                emit(emptyPreferences())
            } else {
                throw exception
            }
        }.map { preferences ->
            preferences[KEY_IS_NIGHT_MODE]
        }

    suspend fun saveFavoriteColor(color: String) {
        dataStore.edit { preferences ->
            preferences[KEY_FAVORITE_COLOR] = color
        }
    }

    suspend fun saveIsNightMode(isNightMode: Boolean) {
        dataStore.edit { preferences ->
            preferences[KEY_IS_NIGHT_MODE] = isNightMode
        }
    }

    companion object {
        @Volatile
        private var instance: UserPreferences? = null

        fun getInstance(context: Context): UserPreferences {
            return instance ?: synchronized(this) {
                instance ?: UserPreferences(context).also {
                    instance = it
                }
            }
        }

        private const val KEY_FAVORITE_COLOR = "favorite_color"
        private const val KEY_IS_NIGHT_MODE = "is_night_mode"
    }
}

然后,我们可以为每个首选项观察者创建单独的流:

class MyViewModel(application: Application) : AndroidViewModel(application) {

    private val userPreferences = UserPreferences.getInstance(application)

    val favoriteColorFlow = userPreferences.favoriteColorFlow.asLiveData()
    val isNightModeFlow = userPreferences.isNightModeFlow.asLiveData()

    fun saveFavoriteColor(color: String) = viewModelScope.launch {
        userPreferences

相关内容

热门资讯

总算明白!wepoker免费脚... 总算明白!wepoker免费脚本咨询,云扑克有透视吗,新2025版(有挂技巧);wepoker免费脚...
透视最新!aapoker脚本怎... 透视最新!aapoker脚本怎么用(透视)原来存在有挂(详细辅助可靠教程)一、aapoker脚本怎么...
研究成果!wepoker辅助器... 研究成果!wepoker辅助器官方,sohoo辅助,2025版教程(有挂方法)这是由厦门游乐互动科技...
透视肯定!wepoker透视底... 透视肯定!wepoker透视底牌(透视)确实真的有挂(详细辅助揭秘教程);1、打开软件启动之后找到中...
分辨真假!wepoker免费脚... 分辨真假!wepoker免费脚本咨询,云扑克有透视吗,教你攻略(有挂教程)1、很好的工具软件,可以解...
透视智能ai!佛手大菠萝13道... 透视智能ai!佛手大菠萝13道挂哪里(透视)果然存在有挂(详细辅助可靠教程)1、完成的残局,帮助玩家...
记者揭秘!哈糖大菠萝破解器,p... 记者揭秘!哈糖大菠萝破解器,pokermaster辅助器,透明挂教程(有挂攻略)是一款可以让一直输的...
透视系统!哈糖大菠萝挂(透视)... 透视系统!哈糖大菠萝挂(透视)其实是真的有挂(详细辅助规律教程)1、下载好辅助软件之后点击打开,先需...
推荐几款新版!wpk插件,we... 推荐几款新版!wpk插件,wejoker透视方法,新2025教程(有挂软件)是一款可以让一直输的玩家...
辅助透视!德州机器人代打脚本(... 辅助透视!德州机器人代打脚本(透视)一直真的是有挂(详细辅助技巧教程);1、ai辅助优化,发牌逻辑科...