优化
This commit is contained in:
@@ -214,12 +214,17 @@ fun <T> Flow<T>.flowWithLifecycleAndDatabaseChange(
|
|||||||
minActiveState: Lifecycle.State = Lifecycle.State.STARTED,
|
minActiveState: Lifecycle.State = Lifecycle.State.STARTED,
|
||||||
table: String
|
table: String
|
||||||
): Flow<T> = callbackFlow {
|
): Flow<T> = callbackFlow {
|
||||||
|
var update = 0
|
||||||
val channel = appDb.invalidationTrackerFlow(table)
|
val channel = appDb.invalidationTrackerFlow(table)
|
||||||
.conflate()
|
.conflate()
|
||||||
|
.onEach { update++ }
|
||||||
.produceIn(this)
|
.produceIn(this)
|
||||||
lifecycle.repeatOnLifecycle(minActiveState) {
|
lifecycle.repeatOnLifecycle(minActiveState) {
|
||||||
channel.receive()
|
if (update == 0) {
|
||||||
|
channel.receive()
|
||||||
|
}
|
||||||
this@flowWithLifecycleAndDatabaseChange.collect {
|
this@flowWithLifecycleAndDatabaseChange.collect {
|
||||||
|
update = 0
|
||||||
send(it)
|
send(it)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -231,16 +236,21 @@ fun <T> Flow<T>.flowWithLifecycleAndDatabaseChangeFirst(
|
|||||||
minActiveState: Lifecycle.State = Lifecycle.State.STARTED,
|
minActiveState: Lifecycle.State = Lifecycle.State.STARTED,
|
||||||
table: String
|
table: String
|
||||||
): Flow<T> = callbackFlow {
|
): Flow<T> = callbackFlow {
|
||||||
|
var update = 0
|
||||||
val isActive = lifecycle.currentState.isAtLeast(minActiveState)
|
val isActive = lifecycle.currentState.isAtLeast(minActiveState)
|
||||||
val channel = appDb.invalidationTrackerFlow(table, emitInitialState = isActive)
|
val channel = appDb.invalidationTrackerFlow(table, emitInitialState = isActive)
|
||||||
.conflate()
|
.conflate()
|
||||||
|
.onEach { update++ }
|
||||||
.produceIn(this)
|
.produceIn(this)
|
||||||
if (!isActive) {
|
if (!isActive) {
|
||||||
send(first())
|
send(first())
|
||||||
}
|
}
|
||||||
lifecycle.repeatOnLifecycle(minActiveState) {
|
lifecycle.repeatOnLifecycle(minActiveState) {
|
||||||
channel.receive()
|
if (update == 0) {
|
||||||
|
channel.receive()
|
||||||
|
}
|
||||||
this@flowWithLifecycleAndDatabaseChangeFirst.collect {
|
this@flowWithLifecycleAndDatabaseChangeFirst.collect {
|
||||||
|
update = 0
|
||||||
send(it)
|
send(it)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user