Skip to content

Commit

Permalink
Correct StateFlow not updating with new List
Browse files Browse the repository at this point in the history
Because the actual List pointer is the same,
 there is no difference between the value sent to emit and the
 value still in the StateFlow.

To correct this,
 A new list is created by appending an immutable list
 with the new value, which is then saved internally and emitted.

In response to work done:
streetcomplete#5482
  • Loading branch information
Doomsdayrs committed Feb 15, 2024
1 parent 1cce1a0 commit 0ffddeb
Showing 1 changed file with 2 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -59,14 +59,14 @@ class LogsViewModelImpl(
filters.transformLatest { filters ->
// get prior logs into a backing state
// There will be duplication regardless.
val logs = ArrayList(logsController.getLogs(filters))
var logs: List<LogMessage> = logsController.getLogs(filters)

// emit the logs for the first view
emit(logs)

// start listening to new logs
getIncomingLogs(filters).collect {
logs.add(it)
logs = logs + it
emit(logs)
}
}.stateIn(viewModelScope + Dispatchers.IO, SharingStarted.Eagerly, emptyList())
Expand Down

0 comments on commit 0ffddeb

Please sign in to comment.