From 0bc0eb8e19665bcdaca18961d31078379c3f2a58 Mon Sep 17 00:00:00 2001 From: Benoit Martins Date: Fri, 23 May 2025 15:50:27 +0200 Subject: [PATCH] update CoreContext.doOnCoreQueue --- Linphone/Contacts/ContactsManager.swift | 4 +-- Linphone/Core/CoreContext.swift | 42 +++++++++++++++---------- 2 files changed, 27 insertions(+), 19 deletions(-) diff --git a/Linphone/Contacts/ContactsManager.swift b/Linphone/Contacts/ContactsManager.swift index 6a7eeab6f..7a1c807aa 100644 --- a/Linphone/Contacts/ContactsManager.swift +++ b/Linphone/Contacts/ContactsManager.swift @@ -88,7 +88,7 @@ final class ContactsManager: ObservableObject { linphoneFriendList.displayName = self.linphoneAddressBookFriendList core.addFriendList(list: linphoneFriendList) } - linphoneFriendList.subscriptionsEnabled = true + core.friendListSubscriptionEnabled = true } } @@ -383,8 +383,6 @@ final class ContactsManager: ObservableObject { } func addFriendListDelegate() { - self.linphoneFriendList?.updateSubscriptions() - self.friendList?.updateSubscriptions() CoreContext.shared.mCore.friendsLists.forEach { friendList in friendList.updateSubscriptions() } diff --git a/Linphone/Core/CoreContext.swift b/Linphone/Core/CoreContext.swift index cfad9d5e9..9d6e594df 100644 --- a/Linphone/Core/CoreContext.swift +++ b/Linphone/Core/CoreContext.swift @@ -67,21 +67,33 @@ final class CoreContext: ObservableObject { } func doOnCoreQueue(synchronous: Bool = false, lambda: @escaping (Core) -> Void) { - if synchronous { - coreQueue.sync { - lambda(self.mCore) - } - } else { - coreQueue.async { - if self.mCore.globalState != .Off { - lambda(self.mCore) - } else { - Log.warn("Doesn't run the asynchronous function because the core is off") - } + let isOnQueue = DispatchQueue.getSpecific(key: coreQueueKey) != nil + + let execute = { + guard self.mCore.globalState != .Off else { + Log.warn("Skipped execution: core is off") + return } + lambda(self.mCore) + } + + switch (synchronous, isOnQueue) { + case (true, true), (false, true): + // Already on the queue → run directly + execute() + case (true, false): + coreQueue.sync(execute: execute) + case (false, false): + coreQueue.async(execute: execute) } } + private let coreQueueKey: DispatchSpecificKey = { + let key = DispatchSpecificKey() + coreQueue.setSpecific(key: key, value: ()) + return key + }() + func initialiseCore() throws { Log.info("Initialising core") #if USE_CRASHLYTICS @@ -145,7 +157,6 @@ final class CoreContext: ObservableObject { self.mCore.videoDisplayEnabled = true self.mCore.videoPreviewEnabled = false self.mCore.fecEnabled = true - self.mCore.friendListSubscriptionEnabled = true // Migration self.mCore.config!.setBool(section: "sip", key: "auto_answer_replacing_calls", value: false) @@ -230,6 +241,7 @@ final class CoreContext: ObservableObject { self.coreIsStarted = state == GlobalState.On } } + }, onCallStateChanged: { (core: Core, call: Call, cstate: Call.State, message: String) in TelecomManager.shared.onCallStateChanged(core: core, call: call, state: cstate, message: message) @@ -384,13 +396,11 @@ final class CoreContext: ObservableObject { } func onEnterForeground() { - coreQueue.sync { + coreQueue.async { // We can't rely on defaultAccount?.params?.isPublishEnabled // as it will be modified by the SDK when changing the presence status try? self.mCore.start() - Log.info("App is in foreground, PUBLISHING presence as Online") - self.updatePresence(core: self.mCore, presence: ConsolidatedPresence.Online) } } @@ -416,7 +426,7 @@ final class CoreContext: ObservableObject { } func performActionOnCoreQueueWhenCoreIsStarted(action: @escaping (_ core: Core) -> Void ) { - if coreHasStartedOnce { + if coreIsStarted { CoreContext.shared.doOnCoreQueue { core in action(core) }