Android 首选项数据存储流不发出相同的值

发布于 2025-01-18 04:53:41 字数 2349 浏览 6 评论 0原文

只是测试 Preferences DataStore 并发现提供的 Flow 输出不会发出相同的值,我的设置如下:

DataStore Util Class:

object DataStore {
    private val Context.settings by preferencesDataStore("settings") 

    suspend fun saveBoolean(context: Context, keyResId: Int, value: Boolean) {
        val key = booleanPreferencesKey(context.getString(keyResId))

        context.settings.edit {
            it[key] = value
        }
    }

    fun getBooleanFlow(context: Context, keyResId: Int, defaultValueResId: Int): Flow<Boolean> {
        val key = booleanPreferencesKey(context.getString(keyResId))
        val defaultValue = context.resources.getBoolean(defaultValueResId)

        return context.settings.data.map {
            it[key] ?: defaultValue
        }
    }
}

< strong>ViewModel 类:

class FirstViewModel(application: Application) : AndroidViewModel(application) {
    private val uiScope = viewModelScope

    val isUpdateAvailable = DataStore.getBooleanFlow(
        getApplication(), R.string.is_update_available_key, R.bool.is_update_available_default
    )

    fun updateIsUpdateAvailable() = uiScope.launch {
        DataStore.saveBoolean(getApplication(), R.string.is_update_available_key, true)  //<- always set to true
    }
}

Fragment 类:

class FirstFragment : Fragment() {
    private lateinit var binding: FragmentFirstBinding
    private lateinit var viewModel: FirstViewModel

    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View {
        binding = DataBindingUtil.inflate(inflater, R.layout.fragment_first, container, false)
        viewModel = ViewModelProvider(this).get(FirstViewModel::class.java)

        lifecycleScope.launch {
            viewLifecycleOwner.repeatOnLifecycle(Lifecycle.State.STARTED) {
                viewModel.isUpdateAvailable.collect {
                    Log.v("xxx", "isUpdateAvailable: $it")
                }
            }
        }

        binding.saveButton.setOnClickListener {
            viewModel.updateIsUpdateAvailable()
        }

        return binding.root
    }
}

由于我每次都保存 true,而 Log 仅显示一次,这意味着 Flow 不会发出相同的值。我说得对吗?这是故意行为吗?

Just testing Preferences DataStore and found out that the provided Flow output won't emit same value, my setup as followed:

DataStore Util Class:

object DataStore {
    private val Context.settings by preferencesDataStore("settings") 

    suspend fun saveBoolean(context: Context, keyResId: Int, value: Boolean) {
        val key = booleanPreferencesKey(context.getString(keyResId))

        context.settings.edit {
            it[key] = value
        }
    }

    fun getBooleanFlow(context: Context, keyResId: Int, defaultValueResId: Int): Flow<Boolean> {
        val key = booleanPreferencesKey(context.getString(keyResId))
        val defaultValue = context.resources.getBoolean(defaultValueResId)

        return context.settings.data.map {
            it[key] ?: defaultValue
        }
    }
}

ViewModel Class:

class FirstViewModel(application: Application) : AndroidViewModel(application) {
    private val uiScope = viewModelScope

    val isUpdateAvailable = DataStore.getBooleanFlow(
        getApplication(), R.string.is_update_available_key, R.bool.is_update_available_default
    )

    fun updateIsUpdateAvailable() = uiScope.launch {
        DataStore.saveBoolean(getApplication(), R.string.is_update_available_key, true)  //<- always set to true
    }
}

Fragment Class:

class FirstFragment : Fragment() {
    private lateinit var binding: FragmentFirstBinding
    private lateinit var viewModel: FirstViewModel

    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View {
        binding = DataBindingUtil.inflate(inflater, R.layout.fragment_first, container, false)
        viewModel = ViewModelProvider(this).get(FirstViewModel::class.java)

        lifecycleScope.launch {
            viewLifecycleOwner.repeatOnLifecycle(Lifecycle.State.STARTED) {
                viewModel.isUpdateAvailable.collect {
                    Log.v("xxx", "isUpdateAvailable: $it")
                }
            }
        }

        binding.saveButton.setOnClickListener {
            viewModel.updateIsUpdateAvailable()
        }

        return binding.root
    }
}

Since I'm saving true each time, and the Log just shows once, which means the Flow doesn't emit same value. Am I correct? Is this intentional behavior?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

坐在坟头思考人生 2025-01-25 04:53:41

对,context.settings.data流不发出相同的值。我没有找到任何证实的文档,但是挖掘 datastore 库的来源表明,如果当前值等于新值,则不会发生发射。更新值的函数的源代码:

private val downstreamFlow = MutableStateFlow(...)


private suspend fun transformAndWrite(
    transform: suspend (t: T) -> T,
    callerContext: CoroutineContext
): T {
    
    val curDataAndHash = downstreamFlow.value as Data<T>
    curDataAndHash.checkHashCode()

    val curData = curDataAndHash.value
    val newData = withContext(callerContext) { transform(curData) }

    curDataAndHash.checkHashCode()

    // here comparison is happening
    return if (curData == newData) {
        curData
    } else {
        // if curData and newData are not equal save and emit newData
        writeData(newData)
        downstreamFlow.value = Data(newData, newData.hashCode())
        newData
    }
}

Right, context.settings.data flow doesn't emit the same value. I haven't found any docs confirming that, but digging into the sources of DataStore library shows that if the current value is equal to the new value, then emitting doesn't happen. The source code of a function that updates the value:

private val downstreamFlow = MutableStateFlow(...)


private suspend fun transformAndWrite(
    transform: suspend (t: T) -> T,
    callerContext: CoroutineContext
): T {
    
    val curDataAndHash = downstreamFlow.value as Data<T>
    curDataAndHash.checkHashCode()

    val curData = curDataAndHash.value
    val newData = withContext(callerContext) { transform(curData) }

    curDataAndHash.checkHashCode()

    // here comparison is happening
    return if (curData == newData) {
        curData
    } else {
        // if curData and newData are not equal save and emit newData
        writeData(newData)
        downstreamFlow.value = Data(newData, newData.hashCode())
        newData
    }
}
樱桃奶球 2025-01-25 04:53:41

我为此做了一个解决方法。

  1. 将属性的先前值存储在变量中
private val dataStore = context.dataStore  
private var previousConfig: AppConfig = AppConfig.default() //Any default/emptyvalue

     val preferencesFlow = dataStore.data
        .catch { exception ->
            Log.e(TAG, "Error reading preferences", exception)
            emit(emptyPreferences())
        }
        .map { preferences ->
            val result = preferences[CONFIG]?.let {
                moshiAdapter.fromJson(it) ?: AppConfig.default()
            } ?: AppConfig.default()
            previousConfig = result //here you update your previous value
            return@map result
        }
  1. 每次要更新数据时,首先将新值与先前值进行比较。
  2. 如果数据相同,则在推送实际的新数据之前,您会推送一些假数据,这些假数据保证与新数据不同。
    这也可以通过在配置数据类中引入一些额外的变量来实现。该变量将专门用于在比较数据类实例时使 equal() 返回 false。然后您可以推送新数据(请参阅下面的代码)。
suspend fun updateConfig(appConfig: AppConfig) {
        if (appConfig == previousConfig) {
            dataStore
                .edit { preferences -> // pushing fake data
                    preferences[PreferencesKeys.CONFIG] = moshiAdapter
.toJson(appConfig.copy(equalityIndicator++))
                }
        }
        dataStore
            .edit { preferences -> //now pushing the real valid new data
                preferences[PreferencesKeys.CONFIG] = moshiAdapter.toJson(appConfig)
            }
    }

这是一个丑陋的解决方法,但它确实有效。我希望谷歌有一天能为我们提供更好的方法。

I made a workaround for this.

  1. You store the previous value of the properties in a variable
private val dataStore = context.dataStore  
private var previousConfig: AppConfig = AppConfig.default() //Any default/emptyvalue

     val preferencesFlow = dataStore.data
        .catch { exception ->
            Log.e(TAG, "Error reading preferences", exception)
            emit(emptyPreferences())
        }
        .map { preferences ->
            val result = preferences[CONFIG]?.let {
                moshiAdapter.fromJson(it) ?: AppConfig.default()
            } ?: AppConfig.default()
            previousConfig = result //here you update your previous value
            return@map result
        }
  1. Each time you want to update data, you first compare the new value with the previous one.
  2. And if data is the same, before pushing your actual new data, you push some fake data which is guaranteed not same as the new data.
    This can be also achieved by introducing some extra variable in your configuration data class. This variable will serve exlusively to make equal() return false when comparing data class instances. Then you can push your new data (see code below).
suspend fun updateConfig(appConfig: AppConfig) {
        if (appConfig == previousConfig) {
            dataStore
                .edit { preferences -> // pushing fake data
                    preferences[PreferencesKeys.CONFIG] = moshiAdapter
.toJson(appConfig.copy(equalityIndicator++))
                }
        }
        dataStore
            .edit { preferences -> //now pushing the real valid new data
                preferences[PreferencesKeys.CONFIG] = moshiAdapter.toJson(appConfig)
            }
    }

It's an ugly workaround but it works. I hope Google will offer us some better way sometime.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文