Revert "ROOM patch: refactoring"

This reverts commit 3a0083ad44.
This commit is contained in:
M66B 2024-05-13 17:26:18 +02:00
parent a88379a09d
commit 048c558ebd
2 changed files with 84 additions and 77 deletions

View File

@ -23,7 +23,6 @@ import java.lang.RuntimeException
import java.util.concurrent.Callable import java.util.concurrent.Callable
import java.util.concurrent.Executor import java.util.concurrent.Executor
import java.util.concurrent.atomic.AtomicBoolean import java.util.concurrent.atomic.AtomicBoolean
import java.util.concurrent.atomic.AtomicInteger
/** /**
* A LiveData implementation that closely works with [InvalidationTracker] to implement * A LiveData implementation that closely works with [InvalidationTracker] to implement
@ -55,20 +54,21 @@ internal class RoomTrackingLiveData<T> (
val invalid = AtomicBoolean(true) val invalid = AtomicBoolean(true)
val computing = AtomicBoolean(false) val computing = AtomicBoolean(false)
val registeredObserver = AtomicBoolean(false) val registeredObserver = AtomicBoolean(false)
val queued = AtomicInteger(0); val queued = eu.faircode.email.ObjectHolder<Int>(0)
val lock = Object()
val refreshRunnable = Runnable { val refreshRunnable = Runnable {
synchronized(lock) {
queued.value--
if (queued.value < 0) {
eu.faircode.email.Log.e("$computeFunction queued=" + queued.value)
queued.value = 0
}
}
if (registeredObserver.compareAndSet(false, true)) { if (registeredObserver.compareAndSet(false, true)) {
database.invalidationTracker.addWeakObserver(observer) database.invalidationTracker.addWeakObserver(observer)
} }
val v = queued.decrementAndGet();
if (v < 0) {
queued.set(0)
eu.faircode.email.Log.e("$computeFunction queued=$v")
} else if (v > 0)
eu.faircode.email.Log.persist(eu.faircode.email.EntityLog.Type.Debug1, "$computeFunction queued=$v")
if (v <= 0) {
var value: T? = null var value: T? = null
var computed = false var computed = false
synchronized(computeFunction) { synchronized(computeFunction) {
@ -94,23 +94,30 @@ internal class RoomTrackingLiveData<T> (
postValue(value) postValue(value)
} }
} }
}
val invalidationRunnable = Runnable { val invalidationRunnable = Runnable {
val isActive = hasActiveObservers() val isActive = hasActiveObservers()
if (isActive) { if (isActive) {
queued.incrementAndGet() synchronized(lock) {
if (queued.value > 0) {
eu.faircode.email.Log.persist(eu.faircode.email.EntityLog.Type.Debug1, "$computeFunction queued=" + queued.value)
} else {
queued.value++
queryExecutor.execute(refreshRunnable) queryExecutor.execute(refreshRunnable)
} }
} }
}
}
@Suppress("UNCHECKED_CAST") @Suppress("UNCHECKED_CAST")
override fun onActive() { override fun onActive() {
super.onActive() super.onActive()
container.onActive(this as LiveData<Any>) container.onActive(this as LiveData<Any>)
queued.incrementAndGet(); synchronized(lock) {
queued.value++
queryExecutor.execute(refreshRunnable) queryExecutor.execute(refreshRunnable)
} }
}
@Suppress("UNCHECKED_CAST") @Suppress("UNCHECKED_CAST")
override fun onInactive() { override fun onInactive() {

View File

@ -21,22 +21,15 @@ index 38067b702f..76cf95815c 100644
} }
if (invalidatedTableIds.isNotEmpty()) { if (invalidatedTableIds.isNotEmpty()) {
diff --git a/app/src/main/java/androidx/room/RoomTrackingLiveData.kt b/app/src/main/java/androidx/room/RoomTrackingLiveData.kt diff --git a/app/src/main/java/androidx/room/RoomTrackingLiveData.kt b/app/src/main/java/androidx/room/RoomTrackingLiveData.kt
index 171b57d16e..32317b296a 100644 index 171b57d16e..85c14c1a93 100644
--- a/app/src/main/java/androidx/room/RoomTrackingLiveData.kt --- a/app/src/main/java/androidx/room/RoomTrackingLiveData.kt
+++ b/app/src/main/java/androidx/room/RoomTrackingLiveData.kt +++ b/app/src/main/java/androidx/room/RoomTrackingLiveData.kt
@@ -23,6 +23,7 @@ import java.lang.RuntimeException @@ -54,62 +54,70 @@ internal class RoomTrackingLiveData<T> (
import java.util.concurrent.Callable
import java.util.concurrent.Executor
import java.util.concurrent.atomic.AtomicBoolean
+import java.util.concurrent.atomic.AtomicInteger
/**
* A LiveData implementation that closely works with [InvalidationTracker] to implement
@@ -54,62 +55,62 @@ internal class RoomTrackingLiveData<T> (
val invalid = AtomicBoolean(true) val invalid = AtomicBoolean(true)
val computing = AtomicBoolean(false) val computing = AtomicBoolean(false)
val registeredObserver = AtomicBoolean(false) val registeredObserver = AtomicBoolean(false)
+ val queued = AtomicInteger(0); + val queued = eu.faircode.email.ObjectHolder<Int>(0)
+ val lock = Object()
val refreshRunnable = Runnable { val refreshRunnable = Runnable {
- if (registeredObserver.compareAndSet(false, true)) { - if (registeredObserver.compareAndSet(false, true)) {
- database.invalidationTracker.addWeakObserver(observer) - database.invalidationTracker.addWeakObserver(observer)
@ -77,18 +70,18 @@ index 171b57d16e..32317b296a 100644
- // We've left invalid in set state. The check below recovers. - // We've left invalid in set state. The check below recovers.
- } while (computed && invalid.get()) - } while (computed && invalid.get())
- } - }
+ synchronized(lock) {
+ queued.value--
+ if (queued.value < 0) {
+ eu.faircode.email.Log.e("$computeFunction queued=" + queued.value)
+ queued.value = 0
+ }
+ }
+
+ if (registeredObserver.compareAndSet(false, true)) { + if (registeredObserver.compareAndSet(false, true)) {
+ database.invalidationTracker.addWeakObserver(observer) + database.invalidationTracker.addWeakObserver(observer)
+ } + }
+ +
+ val v = queued.decrementAndGet();
+ if (v < 0) {
+ queued.set(0)
+ eu.faircode.email.Log.e("$computeFunction queued=$v")
+ } else if (v > 0)
+ eu.faircode.email.Log.persist(eu.faircode.email.EntityLog.Type.Debug1, "$computeFunction queued=$v")
+
+ if (v <= 0) {
+ var value: T? = null + var value: T? = null
+ var computed = false + var computed = false
+ synchronized(computeFunction) { + synchronized(computeFunction) {
@ -113,7 +106,6 @@ index 171b57d16e..32317b296a 100644
+ if (computed) { + if (computed) {
+ postValue(value) + postValue(value)
+ } + }
+ }
+ } + }
val invalidationRunnable = Runnable { val invalidationRunnable = Runnable {
@ -126,9 +118,15 @@ index 171b57d16e..32317b296a 100644
- } - }
+ val isActive = hasActiveObservers() + val isActive = hasActiveObservers()
+ if (isActive) { + if (isActive) {
+ queued.incrementAndGet() + synchronized(lock) {
+ if (queued.value > 0) {
+ eu.faircode.email.Log.persist(eu.faircode.email.EntityLog.Type.Debug, "$computeFunction queued=" + queued.value)
+ } else {
+ queued.value++
+ queryExecutor.execute(refreshRunnable) + queryExecutor.execute(refreshRunnable)
+ } + }
+ }
+ }
+ } + }
@Suppress("UNCHECKED_CAST") @Suppress("UNCHECKED_CAST")
@ -139,8 +137,10 @@ index 171b57d16e..32317b296a 100644
- } - }
+ super.onActive() + super.onActive()
+ container.onActive(this as LiveData<Any>) + container.onActive(this as LiveData<Any>)
+ queued.incrementAndGet(); + synchronized(lock) {
+ queued.value++
+ queryExecutor.execute(refreshRunnable) + queryExecutor.execute(refreshRunnable)
+ }
+ } + }
@Suppress("UNCHECKED_CAST") @Suppress("UNCHECKED_CAST")