From 0179181ced454e80e44271eb2ec4c6b4c120a29b Mon Sep 17 00:00:00 2001 From: Dmytro Date: Mon, 10 Nov 2025 15:02:42 +0100 Subject: [PATCH 1/2] add `mutex.withLock` in `removeKeysStartingWith` --- .../mobile_core/storage/Repository.kt | 23 ++++++++++++------- .../mobile_core/storage/SettingsExt.kt | 10 ++++++-- 2 files changed, 23 insertions(+), 10 deletions(-) diff --git a/core/src/commonMain/kotlin/com/sourcepoint/mobile_core/storage/Repository.kt b/core/src/commonMain/kotlin/com/sourcepoint/mobile_core/storage/Repository.kt index cc52887f..147cbdc4 100644 --- a/core/src/commonMain/kotlin/com/sourcepoint/mobile_core/storage/Repository.kt +++ b/core/src/commonMain/kotlin/com/sourcepoint/mobile_core/storage/Repository.kt @@ -3,6 +3,7 @@ package com.sourcepoint.mobile_core.storage import com.russhwolf.settings.Settings import com.sourcepoint.mobile_core.models.consents.IABData import com.sourcepoint.mobile_core.models.consents.State +import kotlinx.coroutines.runBlocking import kotlinx.serialization.encodeToString import kotlinx.serialization.json.Json @@ -21,8 +22,10 @@ class Repository(private val storage: Settings) { .filter { it.startsWith(TCF_PREFIX) } .associateWith { storage[it]!! } set(value) { - storage.removeKeysStartingWith(prefix = TCF_PREFIX) - value.entries.forEach { storage[it.key] = it.value } + runBlocking { + storage.removeKeysStartingWith(prefix = TCF_PREFIX) + value.entries.forEach { storage[it.key] = it.value } + } } var gppData: IABData @@ -30,8 +33,10 @@ class Repository(private val storage: Settings) { .filter { it.startsWith(GPP_PREFIX) } .associateWith { storage[it]!! } set(value) { - storage.removeKeysStartingWith(prefix = GPP_PREFIX) - value.entries.forEach { storage[it.key] = it.value } + runBlocking { + storage.removeKeysStartingWith(prefix = GPP_PREFIX) + value.entries.forEach { storage[it.key] = it.value } + } } var uspString: String? @@ -45,9 +50,11 @@ class Repository(private val storage: Settings) { set(value) { storage[SP_STATE_KEY] = Json.encodeToString(value) } fun clear() { - storage.removeKeysStartingWith(prefix = TCF_PREFIX) - storage.removeKeysStartingWith(prefix = GPP_PREFIX) - storage.remove(USPSTRING_KEY) - storage.remove(SP_STATE_KEY) + runBlocking { + storage.removeKeysStartingWith(prefix = TCF_PREFIX) + storage.removeKeysStartingWith(prefix = GPP_PREFIX) + storage.remove(USPSTRING_KEY) + storage.remove(SP_STATE_KEY) + } } } diff --git a/core/src/commonMain/kotlin/com/sourcepoint/mobile_core/storage/SettingsExt.kt b/core/src/commonMain/kotlin/com/sourcepoint/mobile_core/storage/SettingsExt.kt index a009c213..1abfa8aa 100644 --- a/core/src/commonMain/kotlin/com/sourcepoint/mobile_core/storage/SettingsExt.kt +++ b/core/src/commonMain/kotlin/com/sourcepoint/mobile_core/storage/SettingsExt.kt @@ -1,6 +1,7 @@ package com.sourcepoint.mobile_core.storage import com.russhwolf.settings.Settings +import kotlinx.coroutines.sync.withLock import com.russhwolf.settings.set as originalSet import com.russhwolf.settings.get as originalGet import kotlinx.serialization.json.JsonNull @@ -16,8 +17,13 @@ import kotlinx.serialization.json.intOrNull import kotlinx.serialization.json.long import kotlinx.serialization.json.longOrNull -internal fun Settings.removeKeysStartingWith(prefix: String) { - keys.filter { it.startsWith(prefix) }.forEach { remove(it) } +private val settingsMutex = kotlinx.coroutines.sync.Mutex() + +internal suspend fun Settings.removeKeysStartingWith(prefix: String) { + settingsMutex.withLock { + val toRemove = keys.filter { it.startsWith(prefix) } + toRemove.forEach { remove(it) } + } } internal operator fun Settings.set(key: String, value: JsonPrimitive) = putJsonPrimitive(key, value) From 5c648d50da3496fcacb7c1b4b61248015c3011d6 Mon Sep 17 00:00:00 2001 From: Dmytro Date: Thu, 13 Nov 2025 13:15:43 +0100 Subject: [PATCH 2/2] move `runBlocking` in `removeKeysStartingWith` --- .../mobile_core/storage/Repository.kt | 24 +++++++------------ .../mobile_core/storage/SettingsExt.kt | 11 +++++---- 2 files changed, 15 insertions(+), 20 deletions(-) diff --git a/core/src/commonMain/kotlin/com/sourcepoint/mobile_core/storage/Repository.kt b/core/src/commonMain/kotlin/com/sourcepoint/mobile_core/storage/Repository.kt index 147cbdc4..bfeb28b1 100644 --- a/core/src/commonMain/kotlin/com/sourcepoint/mobile_core/storage/Repository.kt +++ b/core/src/commonMain/kotlin/com/sourcepoint/mobile_core/storage/Repository.kt @@ -3,8 +3,6 @@ package com.sourcepoint.mobile_core.storage import com.russhwolf.settings.Settings import com.sourcepoint.mobile_core.models.consents.IABData import com.sourcepoint.mobile_core.models.consents.State -import kotlinx.coroutines.runBlocking -import kotlinx.serialization.encodeToString import kotlinx.serialization.json.Json class Repository(private val storage: Settings) { @@ -22,10 +20,8 @@ class Repository(private val storage: Settings) { .filter { it.startsWith(TCF_PREFIX) } .associateWith { storage[it]!! } set(value) { - runBlocking { - storage.removeKeysStartingWith(prefix = TCF_PREFIX) - value.entries.forEach { storage[it.key] = it.value } - } + storage.removeKeysStartingWith(prefix = TCF_PREFIX) + value.entries.forEach { storage[it.key] = it.value } } var gppData: IABData @@ -33,10 +29,8 @@ class Repository(private val storage: Settings) { .filter { it.startsWith(GPP_PREFIX) } .associateWith { storage[it]!! } set(value) { - runBlocking { - storage.removeKeysStartingWith(prefix = GPP_PREFIX) - value.entries.forEach { storage[it.key] = it.value } - } + storage.removeKeysStartingWith(prefix = GPP_PREFIX) + value.entries.forEach { storage[it.key] = it.value } } var uspString: String? @@ -50,11 +44,9 @@ class Repository(private val storage: Settings) { set(value) { storage[SP_STATE_KEY] = Json.encodeToString(value) } fun clear() { - runBlocking { - storage.removeKeysStartingWith(prefix = TCF_PREFIX) - storage.removeKeysStartingWith(prefix = GPP_PREFIX) - storage.remove(USPSTRING_KEY) - storage.remove(SP_STATE_KEY) - } + storage.removeKeysStartingWith(prefix = TCF_PREFIX) + storage.removeKeysStartingWith(prefix = GPP_PREFIX) + storage.remove(USPSTRING_KEY) + storage.remove(SP_STATE_KEY) } } diff --git a/core/src/commonMain/kotlin/com/sourcepoint/mobile_core/storage/SettingsExt.kt b/core/src/commonMain/kotlin/com/sourcepoint/mobile_core/storage/SettingsExt.kt index 1abfa8aa..4d5909ae 100644 --- a/core/src/commonMain/kotlin/com/sourcepoint/mobile_core/storage/SettingsExt.kt +++ b/core/src/commonMain/kotlin/com/sourcepoint/mobile_core/storage/SettingsExt.kt @@ -1,6 +1,7 @@ package com.sourcepoint.mobile_core.storage import com.russhwolf.settings.Settings +import kotlinx.coroutines.runBlocking import kotlinx.coroutines.sync.withLock import com.russhwolf.settings.set as originalSet import com.russhwolf.settings.get as originalGet @@ -19,10 +20,12 @@ import kotlinx.serialization.json.longOrNull private val settingsMutex = kotlinx.coroutines.sync.Mutex() -internal suspend fun Settings.removeKeysStartingWith(prefix: String) { - settingsMutex.withLock { - val toRemove = keys.filter { it.startsWith(prefix) } - toRemove.forEach { remove(it) } +internal fun Settings.removeKeysStartingWith(prefix: String) { + runBlocking { + settingsMutex.withLock { + val toRemove = keys.filter { it.startsWith(prefix) } + toRemove.forEach { remove(it) } + } } }