From 64331eae0789c6a370bb1642896e84113839e824 Mon Sep 17 00:00:00 2001 From: Patrick Mick Date: Thu, 4 Jun 2026 14:52:46 -0700 Subject: [PATCH] Wrap transactions dictionary in ThreadSafe type --- ios/RNIapIosSk2.swift | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/ios/RNIapIosSk2.swift b/ios/RNIapIosSk2.swift index bede028d4..65191448b 100644 --- a/ios/RNIapIosSk2.swift +++ b/ios/RNIapIosSk2.swift @@ -449,7 +449,7 @@ class RNIapIosSk2: RCTEventEmitter, Sk2Delegate { class RNIapIosSk2iOS15: Sk2Delegate { private var _hasListeners = false private let _hasListenersQueue = DispatchQueue(label: "com.dooboolab.rniap.hasListenersQueue", attributes: .concurrent) - private var transactions: [String: Transaction] + private var transactions: ThreadSafe<[String: Transaction]> private var updateListenerTask: Task? private var storefrontListenerTask: Task? fileprivate var sendEvent: ((String?, Any?) -> Void)? @@ -465,7 +465,7 @@ class RNIapIosSk2iOS15: Sk2Delegate { } init(sendEvent: ((String?, Any?) -> Void)? ) { self.sendEvent = sendEvent - transactions = [String: Transaction]() + transactions = ThreadSafe<[String: Transaction]>([:]) } deinit { @@ -524,7 +524,7 @@ class RNIapIosSk2iOS15: Sk2Delegate { func addTransaction(_ transaction: Transaction) { let transactionId = String(transaction.id) - self.transactions[transactionId] = transaction + self.transactions.atomically { $0[transactionId] = transaction } } func listenForStorefrontChanges() -> Task { @@ -597,7 +597,7 @@ class RNIapIosSk2iOS15: Sk2Delegate { Task { await productStore.removeAll() } - transactions.removeAll() + transactions.atomically { $0.removeAll() } removeTransactionObserver() resolve(nil) } @@ -962,11 +962,11 @@ class RNIapIosSk2iOS15: Sk2Delegate { reject: @escaping RCTPromiseRejectBlock = { _, _, _ in } ) { Task { - if let transaction = transactions[transactionIdentifier] { + if let transaction = transactions.value[transactionIdentifier] { debugMessage("Finishing transaction") await transaction.finish() debugMessage("Finished transaction") - transactions.removeValue(forKey: transactionIdentifier) + transactions.atomically { $0.removeValue(forKey: transactionIdentifier) } resolve(nil) } else { reject(IapErrors.E_DEVELOPER_ERROR.rawValue, "Invalid transaction Id", nil) @@ -978,7 +978,7 @@ class RNIapIosSk2iOS15: Sk2Delegate { _ resolve: @escaping RCTPromiseResolveBlock = { _ in }, reject: @escaping RCTPromiseRejectBlock = { _, _, _ in } ) { - resolve(transactions.values.map({(t: Transaction) in serialize(t)})) + resolve(transactions.value.values.map({(t: Transaction) in serialize(t)})) } public func sync( @@ -1048,7 +1048,7 @@ class RNIapIosSk2iOS15: Sk2Delegate { debugMessage("Finishing transaction") await transaction.finish() debugMessage("Finished transaction") - transactions.removeValue(forKey: String(transaction.id)) + transactions.atomically { $0.removeValue(forKey: String(transaction.id)) } } catch { debugMessage("Failed to finish transaction") }