From 9377c902a125f25c5ba6ca6b9a1b23ce864250e3 Mon Sep 17 00:00:00 2001 From: Vitor Pamplona Date: Thu, 9 Nov 2023 14:18:54 -0500 Subject: [PATCH] Moves creation of the LiveData set to a Synchronized block to avoid double creations without destruction. --- .../com/vitorpamplona/amethyst/model/Note.kt | 19 ++++++++++++++++--- .../com/vitorpamplona/amethyst/model/User.kt | 19 ++++++++++++++++--- 2 files changed, 32 insertions(+), 6 deletions(-) diff --git a/app/src/main/java/com/vitorpamplona/amethyst/model/Note.kt b/app/src/main/java/com/vitorpamplona/amethyst/model/Note.kt index 199568c3b..9f35872bf 100644 --- a/app/src/main/java/com/vitorpamplona/amethyst/model/Note.kt +++ b/app/src/main/java/com/vitorpamplona/amethyst/model/Note.kt @@ -634,17 +634,30 @@ open class Note(val idHex: String) { var liveSet: NoteLiveSet? = null + @Synchronized + fun createOrDestroyLiveSync(create: Boolean) { + if (create) { + if (liveSet == null) { + liveSet = NoteLiveSet(this) + } + } else { + if (liveSet != null && liveSet?.isInUse() == false) { + liveSet?.destroy() + liveSet = null + } + } + } + fun live(): NoteLiveSet { if (liveSet == null) { - liveSet = NoteLiveSet(this) + createOrDestroyLiveSync(true) } return liveSet!! } fun clearLive() { if (liveSet != null && liveSet?.isInUse() == false) { - liveSet?.destroy() - liveSet = null + createOrDestroyLiveSync(false) } } diff --git a/app/src/main/java/com/vitorpamplona/amethyst/model/User.kt b/app/src/main/java/com/vitorpamplona/amethyst/model/User.kt index d294a58a8..e2744bbc3 100644 --- a/app/src/main/java/com/vitorpamplona/amethyst/model/User.kt +++ b/app/src/main/java/com/vitorpamplona/amethyst/model/User.kt @@ -359,15 +359,28 @@ class User(val pubkeyHex: String) { fun live(): UserLiveSet { if (liveSet == null) { - liveSet = UserLiveSet(this) + createOrDestroyLiveSync(true) } return liveSet!! } fun clearLive() { if (liveSet != null && liveSet?.isInUse() == false) { - liveSet?.destroy() - liveSet = null + createOrDestroyLiveSync(false) + } + } + + @Synchronized + fun createOrDestroyLiveSync(create: Boolean) { + if (create) { + if (liveSet == null) { + liveSet = UserLiveSet(this) + } + } else { + if (liveSet != null && liveSet?.isInUse() == false) { + liveSet?.destroy() + liveSet = null + } } } }