From 2939012ffa0466193d435f3967a2f446fe58e8c0 Mon Sep 17 00:00:00 2001 From: Horis <8674809+821938089@users.noreply.github.com> Date: Thu, 18 Jul 2024 22:05:14 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/io/legado/app/utils/FlowExtensions.kt | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/app/src/main/java/io/legado/app/utils/FlowExtensions.kt b/app/src/main/java/io/legado/app/utils/FlowExtensions.kt index 5890d4728..d2ce0d674 100644 --- a/app/src/main/java/io/legado/app/utils/FlowExtensions.kt +++ b/app/src/main/java/io/legado/app/utils/FlowExtensions.kt @@ -214,12 +214,17 @@ fun Flow.flowWithLifecycleAndDatabaseChange( minActiveState: Lifecycle.State = Lifecycle.State.STARTED, table: String ): Flow = callbackFlow { + var update = 0 val channel = appDb.invalidationTrackerFlow(table) .conflate() + .onEach { update++ } .produceIn(this) lifecycle.repeatOnLifecycle(minActiveState) { - channel.receive() + if (update == 0) { + channel.receive() + } this@flowWithLifecycleAndDatabaseChange.collect { + update = 0 send(it) } } @@ -231,16 +236,21 @@ fun Flow.flowWithLifecycleAndDatabaseChangeFirst( minActiveState: Lifecycle.State = Lifecycle.State.STARTED, table: String ): Flow = callbackFlow { + var update = 0 val isActive = lifecycle.currentState.isAtLeast(minActiveState) val channel = appDb.invalidationTrackerFlow(table, emitInitialState = isActive) .conflate() + .onEach { update++ } .produceIn(this) if (!isActive) { send(first()) } lifecycle.repeatOnLifecycle(minActiveState) { - channel.receive() + if (update == 0) { + channel.receive() + } this@flowWithLifecycleAndDatabaseChangeFirst.collect { + update = 0 send(it) } }