From 00a7f305a5497f52dd085d9bf98d52f361d92bde Mon Sep 17 00:00:00 2001 From: Benoit Martins Date: Thu, 30 May 2024 11:50:59 +0200 Subject: [PATCH] Check core state before using an asynchronous function Fix markAsRead crash --- Linphone/Contacts/ContactsManager.swift | 3 +-- Linphone/Core/CoreContext.swift | 4 +++- .../Main/Conversations/Model/ConversationModel.swift | 10 +++++++--- .../ViewModel/ConversationViewModel.swift | 9 +++++++-- 4 files changed, 18 insertions(+), 8 deletions(-) diff --git a/Linphone/Contacts/ContactsManager.swift b/Linphone/Contacts/ContactsManager.swift index eba36a0be..ae186df6f 100644 --- a/Linphone/Contacts/ContactsManager.swift +++ b/Linphone/Contacts/ContactsManager.swift @@ -145,8 +145,7 @@ final class ContactsManager: ObservableObject { MagicSearchSingleton.shared.searchForContacts(sourceFlags: MagicSearch.Source.Friends.rawValue | MagicSearch.Source.LdapServers.rawValue) self.friendListSuscription = nil } - - + MagicSearchSingleton.shared.searchForContacts(sourceFlags: MagicSearch.Source.Friends.rawValue | MagicSearch.Source.LdapServers.rawValue) } } diff --git a/Linphone/Core/CoreContext.swift b/Linphone/Core/CoreContext.swift index 81550b380..f3581d75f 100644 --- a/Linphone/Core/CoreContext.swift +++ b/Linphone/Core/CoreContext.swift @@ -64,7 +64,9 @@ final class CoreContext: ObservableObject { } } else { coreQueue.async { - lambda(self.mCore) + if self.mCore.globalState != .Off { + lambda(self.mCore) + } } } } diff --git a/Linphone/UI/Main/Conversations/Model/ConversationModel.swift b/Linphone/UI/Main/Conversations/Model/ConversationModel.swift index 439e22fa9..d98124fb4 100644 --- a/Linphone/UI/Main/Conversations/Model/ConversationModel.swift +++ b/Linphone/UI/Main/Conversations/Model/ConversationModel.swift @@ -109,11 +109,15 @@ class ConversationModel: ObservableObject { func markAsRead() { coreContext.doOnCoreQueue { _ in - self.chatRoom.markAsRead() - let unreadMessagesCountTmp = self.chatRoom.unreadMessagesCount + if unreadMessagesCountTmp > 0 { + self.chatRoom.markAsRead() + } + DispatchQueue.main.async { - self.unreadMessagesCount = unreadMessagesCountTmp + if self.unreadMessagesCount != unreadMessagesCountTmp { + self.unreadMessagesCount = unreadMessagesCountTmp + } } } } diff --git a/Linphone/UI/Main/Conversations/ViewModel/ConversationViewModel.swift b/Linphone/UI/Main/Conversations/ViewModel/ConversationViewModel.swift index 98b228650..43acefef0 100644 --- a/Linphone/UI/Main/Conversations/ViewModel/ConversationViewModel.swift +++ b/Linphone/UI/Main/Conversations/ViewModel/ConversationViewModel.swift @@ -85,10 +85,15 @@ class ConversationViewModel: ObservableObject { func markAsRead() { coreContext.doOnCoreQueue { _ in - self.displayedConversation!.chatRoom.markAsRead() let unreadMessagesCount = self.displayedConversation!.chatRoom.unreadMessagesCount + + if unreadMessagesCount > 0 { + self.displayedConversation!.chatRoom.markAsRead() + } DispatchQueue.main.async { - self.displayedConversationUnreadMessagesCount = unreadMessagesCount + if self.displayedConversationUnreadMessagesCount != unreadMessagesCount { + self.displayedConversationUnreadMessagesCount = unreadMessagesCount + } } } }