Moves creation of the LiveData set to a Synchronized block to avoid double creations without destruction.

This commit is contained in:
Vitor Pamplona
2023-11-09 14:18:54 -05:00
parent 57d97447f4
commit 9377c902a1
2 changed files with 32 additions and 6 deletions
@@ -634,17 +634,30 @@ open class Note(val idHex: String) {
var liveSet: NoteLiveSet? = null var liveSet: NoteLiveSet? = null
fun live(): NoteLiveSet { @Synchronized
fun createOrDestroyLiveSync(create: Boolean) {
if (create) {
if (liveSet == null) { if (liveSet == null) {
liveSet = NoteLiveSet(this) liveSet = NoteLiveSet(this)
} }
} else {
if (liveSet != null && liveSet?.isInUse() == false) {
liveSet?.destroy()
liveSet = null
}
}
}
fun live(): NoteLiveSet {
if (liveSet == null) {
createOrDestroyLiveSync(true)
}
return liveSet!! return liveSet!!
} }
fun clearLive() { fun clearLive() {
if (liveSet != null && liveSet?.isInUse() == false) { if (liveSet != null && liveSet?.isInUse() == false) {
liveSet?.destroy() createOrDestroyLiveSync(false)
liveSet = null
} }
} }
@@ -359,17 +359,30 @@ class User(val pubkeyHex: String) {
fun live(): UserLiveSet { fun live(): UserLiveSet {
if (liveSet == null) { if (liveSet == null) {
liveSet = UserLiveSet(this) createOrDestroyLiveSync(true)
} }
return liveSet!! return liveSet!!
} }
fun clearLive() { fun clearLive() {
if (liveSet != null && liveSet?.isInUse() == false) {
createOrDestroyLiveSync(false)
}
}
@Synchronized
fun createOrDestroyLiveSync(create: Boolean) {
if (create) {
if (liveSet == null) {
liveSet = UserLiveSet(this)
}
} else {
if (liveSet != null && liveSet?.isInUse() == false) { if (liveSet != null && liveSet?.isInUse() == false) {
liveSet?.destroy() liveSet?.destroy()
liveSet = null liveSet = null
} }
} }
}
} }
@Stable @Stable