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 + } } } }