I have an Architecture Components DataStore in my App. In this DataStore I have multiple preferences. Following a snippet of two preferences.
val calibrationFactorFlow: Flow<Float> = context.dataStore.data.map {
preferences -> preferences[PreferencesKeys.CALIBRATION_FACTOR] ?: AppConstants.DEFAULT_CALIBRATION_FACTOR
}
suspend fun saveCalibrationToDataStore(calibrationFactor: Float) {
context.dataStore.edit { preferences ->
preferences[PreferencesKeys.CALIBRATION_FACTOR] = calibrationFactor
}
}
val scaleFactorMetricFlow: Flow<Int> = context.dataStore.data.map {
preferences -> preferences[PreferencesKeys.SCALE_FACTOR_METRIC] ?: AppConstants.DEFAULT_SCALE_FACTOR_METRIC
}
suspend fun saveScaleFactorMetricToDataStore(scaleFactor: Int) {
context.dataStore.edit { preferences ->
preferences[PreferencesKeys.SCALE_FACTOR_METRIC] = scaleFactor
}
}
I use the received Flow in my ViewModel as LiveData.
val calibrationFactor: LiveData<Float> = repository.calibrationFactorFlow.asLiveData()
val scaleSelectedMetricId: LiveData<Int> = repository.scaleFactorMetricFlow.asLiveData()
The problem is, whenever I make an update to one of these preferences in the data store all other preferences receive an update. I cannot unterstand why and I cannot find anything in the documentation.
Someone has any idea why this happens or how I can prevent all preferences from receiving an update when only one preference is updated in LiveData / Flow
Thanks.