I’m currently using DataStore to store and collect values to create a UI.
There are flows for collecting Volume and Media Path.
I understand that when I store a value, it should be collectable in the corresponding Flow, and it currently behaves that way.
However, when I store Volume, the Media Path Flow also emits, which shouldn’t happen.
suspend fun storeVolume(volume: Int) {
dataStore.edit {
it[VOLUME] = volume
}
}
fun getVolume(): Flow<Int> = dataStore.data.map {
it[VOLUME] ?: DEFAULT_VOLUME_SIZE
}
suspend fun storeMediaPath(path: String) {
dataStore.edit {
it[MEDIA] = path
}
}
fun getMediaFile(): Flow<File?> = dataStore.data.map {
it[MEDIA]?.let { path -> File(path) }
}
init {
viewModelScope.launch {
dataStoreHelper.getVolume().collectLatest { volume ->
_mainUiState.update { it.copy(volume = volume) }
}
}
viewModelScope.launch {
dataStoreHelper.getMediaFile().collectLatest { file ->
_mainUiState.update { it.copy(alarmMedia = file) }
}
}
}
I couldn’t find any documentation related to this.
It seems that when I store a value, it gets emitted to all flows collecting that value.
Therefore, it seems to be performing unnecessary tasks, and I would like to optimize this.
philo is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.