Skip to content

Commit 75247c1

Browse files
HS0204criticalAY
authored andcommitted
fix: prevent skipped update in StateFlow
- use collected emission `it` instead of `StateFlow.value` so lastValue tracks only what was actually processed When performing manual deduplication inside a collect block, reading StateFlow.value instead of the emitted value (it) allows the tracking variable to "race ahead" of the collector. If multiple updates occur rapidly (e.g., while the UI is in the background), the tracking variable may be updated to the latest value while processing an intermediate emission. This causes the collector to incorrectly drop the subsequent final emission as a duplicate.
1 parent aecdb30 commit 75247c1

1 file changed

Lines changed: 2 additions & 2 deletions

File tree

  • AnkiDroid/src/main/java/com/ichi2/anki/utils/ext

AnkiDroid/src/main/java/com/ichi2/anki/utils/ext/Flow.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -87,8 +87,8 @@ fun <T> StateFlow<T>.launchCollectionInLifecycleScope(block: suspend (T) -> Unit
8787
activity.lifecycle.repeatOnLifecycle(Lifecycle.State.STARTED) {
8888
this@launchCollectionInLifecycleScope.collect {
8989
// on re-resume, an unchanged value will be emitted for a StateFlow
90-
if (lastValue == value) return@collect
91-
lastValue = value
90+
if (lastValue == it) return@collect
91+
lastValue = it
9292
if (isRobolectric) {
9393
HandlerUtils.postOnNewHandler { runBlocking { block(it) } }
9494
} else {

0 commit comments

Comments
 (0)