From 2b555bb0db9677bfeb56b88c71b4a2c10da73f2a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C3=A9mi=20Latapy?= Date: Thu, 15 Nov 2018 16:54:23 +0100 Subject: [PATCH 1/2] Update LiveData value when becoming active Allow LiveData to stay up to date, especially when resuming an activity --- .../java/io/realm/examples/arch/livemodel/LiveRealmObject.java | 1 + 1 file changed, 1 insertion(+) diff --git a/examples/architectureComponentsExample/src/main/java/io/realm/examples/arch/livemodel/LiveRealmObject.java b/examples/architectureComponentsExample/src/main/java/io/realm/examples/arch/livemodel/LiveRealmObject.java index 97690d596f..58ce10ffce 100644 --- a/examples/architectureComponentsExample/src/main/java/io/realm/examples/arch/livemodel/LiveRealmObject.java +++ b/examples/architectureComponentsExample/src/main/java/io/realm/examples/arch/livemodel/LiveRealmObject.java @@ -87,6 +87,7 @@ protected void onActive() { super.onActive(); T object = getValue(); if (object != null && RealmObject.isValid(object)) { + setValue(object); RealmObject.addChangeListener(object, listener); } } From 0fe3392101a035eb55da88dae33d9e61ffe0a773 Mon Sep 17 00:00:00 2001 From: rlatapy Date: Mon, 3 Dec 2018 00:36:52 +0100 Subject: [PATCH 2/2] store object in the ctor to set it when becoming active for the first time --- .../realm/examples/arch/livemodel/LiveRealmObject.java | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/examples/architectureComponentsExample/src/main/java/io/realm/examples/arch/livemodel/LiveRealmObject.java b/examples/architectureComponentsExample/src/main/java/io/realm/examples/arch/livemodel/LiveRealmObject.java index 58ce10ffce..aeb17225ab 100644 --- a/examples/architectureComponentsExample/src/main/java/io/realm/examples/arch/livemodel/LiveRealmObject.java +++ b/examples/architectureComponentsExample/src/main/java/io/realm/examples/arch/livemodel/LiveRealmObject.java @@ -40,7 +40,9 @@ * @param the type of the RealmModel */ public class LiveRealmObject extends LiveData { - // The listener will listen until the object is deleted. + private final T object; + + // The listener will listen until the object is deleted. // An invalidated object shouldn't be set in LiveData, null is set instead. private RealmObjectChangeListener listener = new RealmObjectChangeListener() { @Override @@ -72,7 +74,7 @@ public LiveRealmObject(@NonNull T object) { if (!RealmObject.isValid(object)) { throw new IllegalArgumentException("The provided RealmObject is no longer valid, and therefore cannot be observed for changes."); } - setValue(object); + this.object = object; } // We should start observing and stop observing, depending on whether we have observers. @@ -89,6 +91,9 @@ protected void onActive() { if (object != null && RealmObject.isValid(object)) { setValue(object); RealmObject.addChangeListener(object, listener); + } else if (RealmObject.isValid(this.object)) { + setValue(this.object); + RealmObject.addChangeListener(this.object, listener); } }