From 0b6fe99183e3a5ec2c90b18f608fb6a907d88863 Mon Sep 17 00:00:00 2001 From: M66B Date: Sat, 15 Jan 2022 14:54:45 +0100 Subject: [PATCH] Stop scanning for prev/next on destroy --- app/src/main/java/eu/faircode/email/ObjectHolder.java | 7 +++++++ .../main/java/eu/faircode/email/ViewModelMessages.java | 10 +++++++++- 2 files changed, 16 insertions(+), 1 deletion(-) diff --git a/app/src/main/java/eu/faircode/email/ObjectHolder.java b/app/src/main/java/eu/faircode/email/ObjectHolder.java index b6f118e776..8d14327a59 100644 --- a/app/src/main/java/eu/faircode/email/ObjectHolder.java +++ b/app/src/main/java/eu/faircode/email/ObjectHolder.java @@ -21,4 +21,11 @@ package eu.faircode.email; public class ObjectHolder { public T value; + + public ObjectHolder() { + } + + public ObjectHolder(T init) { + this.value = init; + } } diff --git a/app/src/main/java/eu/faircode/email/ViewModelMessages.java b/app/src/main/java/eu/faircode/email/ViewModelMessages.java index b1b5089a06..c34b26a351 100644 --- a/app/src/main/java/eu/faircode/email/ViewModelMessages.java +++ b/app/src/main/java/eu/faircode/email/ViewModelMessages.java @@ -272,6 +272,14 @@ public class ViewModelMessages extends ViewModel { return; } + ObjectHolder alive = new ObjectHolder<>(true); + owner.getLifecycle().addObserver(new LifecycleObserver() { + @OnLifecycleEvent(Lifecycle.Event.ON_ANY) + public void onAny() { + alive.value = owner.getLifecycle().getCurrentState().isAtLeast(Lifecycle.State.STARTED); + } + }); + Log.i("Observe previous/next id=" + id); //model.list.getValue().loadAround(lpos); model.list.observe(owner, new Observer>() { @@ -363,7 +371,7 @@ public class ViewModelMessages extends ViewModel { return getPair(plist, ds, count, from + j); } - for (int i = 0; i < count; i += CHUNK_SIZE) { + for (int i = 0; i < count && alive.value; i += CHUNK_SIZE) { Log.i("Observe previous/next load" + " range=" + i + "/#" + count); List messages = ds.loadRange(i, Math.min(CHUNK_SIZE, count - i));