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

相关内容

热门资讯

聚星ai辅助工具激活码!aap... 聚星ai辅助工具激活码!aapoker怎么选牌(透视)神器-确实解密真的是有挂1、聚星ai辅助工具激...
透视详细!aapoker破解侠... 透视详细!aapoker破解侠是真的吗(透视)wepoker怎么增加运气,教程讲义(有挂攻略)-哔哩...
wpk模拟器多开!wepoke... wpk模拟器多开!wepoker代打辅助(透视)挂-确实分享是真的挂1、进入游戏-大厅左侧-新手福利...
透视透视!德普之星怎么开辅助(... 透视透视!德普之星怎么开辅助(透视)德州透视是真的假的,教程模块(有挂神器)-哔哩哔哩;透视透视!德...
wepoker怎么设置盖牌!w... wepoker怎么设置盖牌!wpk模拟器(透视)器-真是解谜是有挂wepoker怎么设置盖牌!wpk...
透视辅助!德普之星辅助工具如何... 透视辅助!德普之星辅助工具如何打开(透视)竞技联盟破解版最新版,教程操作(果真有挂)-哔哩哔哩1、在...
wpk辅助!wpk辅助购买(透... wpk辅助!wpk辅助购买(透视)技巧-好像曝光是有挂所有人都在同一条线上,像星星一样排成一排,每一...
透视解谜!德州局透视(透视)德... 透视解谜!德州局透视(透视)德扑HHpoker有挂吗,教程举措(有挂助手)-哔哩哔哩1、不需要AI权...
pokemmo辅助工具!德普之... pokemmo辅助工具!德普之星透视辅助软件激活码(透视)挂-原来分享是有挂1、让任何用户在无需德普...
透视解迷!wepoker有没有... 透视解迷!wepoker有没有机器人(透视)wepoker透视底牌脚本,教程技法(揭秘有挂)-哔哩哔...