diff --git a/AuthenticatorCore/src/appleMain/kotlin/internal/otp/TotpGenerator.apple.kt b/AuthenticatorCore/src/appleMain/kotlin/internal/otp/TotpGenerator.apple.kt index 3cfafcd..08f9a1f 100644 --- a/AuthenticatorCore/src/appleMain/kotlin/internal/otp/TotpGenerator.apple.kt +++ b/AuthenticatorCore/src/appleMain/kotlin/internal/otp/TotpGenerator.apple.kt @@ -23,6 +23,8 @@ import kotlinx.cinterop.BetaInteropApi import kotlinx.cinterop.ExperimentalForeignApi import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.IO +import kotlinx.coroutines.sync.Mutex +import kotlinx.coroutines.sync.withLock import kotlinx.coroutines.withContext import kotlinx.serialization.json.Json import kotlinx.serialization.json.int @@ -31,6 +33,8 @@ import kotlinx.serialization.json.jsonPrimitive import platform.Foundation.NSData import platform.Foundation.NSUserDefaults +private val writeMutex = Mutex() + @OptIn(BetaInteropApi::class, ExperimentalForeignApi::class) internal actual suspend fun getLegacyAccounts(): List = withContext(Dispatchers.IO) { val userDefaults = NSUserDefaults.standardUserDefaults @@ -51,10 +55,10 @@ internal actual suspend fun getLegacyAccounts(): List = withContext( } @OptIn(ExperimentalForeignApi::class) -internal actual suspend fun deleteLegacyAccount(userId: String) { +internal actual suspend fun deleteLegacyAccount(userId: String) = writeMutex.withLock { withContext(Dispatchers.IO) { val userDefaults = NSUserDefaults.standardUserDefaults - val usersData = userDefaults.objectForKey("ALL_USERS") as? MutableList<*> ?: return@withContext false + val usersData = userDefaults.objectForKey("ALL_USERS") as? MutableList<*> ?: return@withContext val updatedList = usersData.mapNotNull { item -> val data = item as? NSData ?: return@mapNotNull item