Periodically post available live data, even when invalidated

This commit is contained in:
M66B 2019-06-29 21:34:26 +02:00
parent 3669b9ef2e
commit 257d27b2ca
1 changed files with 10 additions and 0 deletions

View File

@ -16,6 +16,8 @@
package androidx.lifecycle;
import android.os.SystemClock;
import androidx.annotation.MainThread;
import androidx.annotation.NonNull;
import androidx.annotation.RestrictTo;
@ -89,14 +91,22 @@ public abstract class ComputableLiveData<T> {
@Override
public void run() {
boolean computed;
long age;
do {
computed = false;
// compute can happen only in 1 thread but no reason to lock others.
if (mComputing.compareAndSet(false, true)) {
// as long as it is invalid, keep computing.
try {
age = SystemClock.elapsedRealtime();
T value = null;
while (mInvalid.compareAndSet(true, false)) {
long now = SystemClock.elapsedRealtime();
if (age + 1500 < now && value != null) {
eu.faircode.email.Log.i(mLiveData + " post age=" + (now - age));
age = now;
mLiveData.postValue(value);
}
computed = true;
value = compute();
}