From 85607166b2b9c8fce49f80e692443bcd37d263c9 Mon Sep 17 00:00:00 2001 From: kele-leanes Date: Wed, 3 Jan 2024 14:53:19 -0300 Subject: [PATCH] Update DispatchGroup to DispatchSemaphore in XMTPModule.swift --- ios/XMTPModule.swift | 30 ++++++++++++++++-------------- 1 file changed, 16 insertions(+), 14 deletions(-) diff --git a/ios/XMTPModule.swift b/ios/XMTPModule.swift index afb26ebe0..0c521997d 100644 --- a/ios/XMTPModule.swift +++ b/ios/XMTPModule.swift @@ -28,8 +28,8 @@ public class XMTPModule: Module { let clientsManager = ClientsManager() let conversationsManager = IsolatedManager() let subscriptionsManager = IsolatedManager>() - var preEnableIdentityCallbackDeferred: DispatchGroup? - var preCreateIdentityCallbackDeferred: DispatchGroup? + private var preEnableIdentityCallbackDeferred: DispatchSemaphore? + private var preCreateIdentityCallbackDeferred: DispatchSemaphore? actor ClientsManager { private var clients: [String: XMTP.Client] = [:] @@ -70,10 +70,10 @@ public class XMTPModule: Module { let signer = ReactNativeSigner(module: self, address: address) self.signer = signer if(hasCreateIdentityCallback ?? false) { - preCreateIdentityCallbackDeferred = DispatchGroup() + preCreateIdentityCallbackDeferred = DispatchSemaphore(value: 0) } if(hasEnableIdentityCallback ?? false) { - preEnableIdentityCallbackDeferred = DispatchGroup() + preEnableIdentityCallbackDeferred = DispatchSemaphore(value: 0) } let preCreateIdentityCallback: PreEventCallback? = hasCreateIdentityCallback ?? false ? self.preCreateIdentityCallback : nil let preEnableIdentityCallback: PreEventCallback? = hasEnableIdentityCallback ?? false ? self.preEnableIdentityCallback : nil @@ -91,10 +91,10 @@ public class XMTPModule: Module { AsyncFunction("createRandom") { (environment: String, appVersion: String?, hasCreateIdentityCallback: Bool?, hasEnableIdentityCallback: Bool?) -> String in let privateKey = try PrivateKey.generate() if(hasCreateIdentityCallback ?? false) { - preCreateIdentityCallbackDeferred = DispatchGroup() + preCreateIdentityCallbackDeferred = DispatchSemaphore(value: 0) } if(hasEnableIdentityCallback ?? false) { - preEnableIdentityCallbackDeferred = DispatchGroup() + preEnableIdentityCallbackDeferred = DispatchSemaphore(value: 0) } let preCreateIdentityCallback: PreEventCallback? = hasCreateIdentityCallback ?? false ? self.preCreateIdentityCallback : nil let preEnableIdentityCallback: PreEventCallback? = hasEnableIdentityCallback ?? false ? self.preEnableIdentityCallback : nil @@ -550,11 +550,15 @@ public class XMTPModule: Module { } Function("preEnableIdentityCallbackCompleted") { - preEnableIdentityCallbackDeferred?.leave() + DispatchQueue.global().async { + self.preEnableIdentityCallbackDeferred?.signal() + } } Function("preCreateIdentityCallbackCompleted") { - preCreateIdentityCallbackDeferred?.leave() + DispatchQueue.global().async { + self.preCreateIdentityCallbackDeferred?.signal() + } } } @@ -695,15 +699,13 @@ public class XMTPModule: Module { func preEnableIdentityCallback() { sendEvent("preEnableIdentityCallback") - preEnableIdentityCallbackDeferred?.enter() - preEnableIdentityCallbackDeferred?.wait() - preCreateIdentityCallbackDeferred = nil + self.preEnableIdentityCallbackDeferred?.wait() + self.preCreateIdentityCallbackDeferred = nil } func preCreateIdentityCallback() { sendEvent("preCreateIdentityCallback") - preCreateIdentityCallbackDeferred?.enter() - preCreateIdentityCallbackDeferred?.wait() - preEnableIdentityCallbackDeferred = nil + self.preCreateIdentityCallbackDeferred?.wait() + self.preEnableIdentityCallbackDeferred = nil } }