diff --git a/Session/Calls/Call Management/SessionCall.swift b/Session/Calls/Call Management/SessionCall.swift index da1324905..7df8004a6 100644 --- a/Session/Calls/Call Management/SessionCall.swift +++ b/Session/Calls/Call Management/SessionCall.swift @@ -278,56 +278,65 @@ public final class SessionCall: CurrentCallProtocol, WebRTCSessionDelegate { let duration: TimeInterval = self.duration let hasStartedConnecting: Bool = self.hasStartedConnecting - // Don't know whether there will be an impact if we don't write async - Storage.shared.write { db in - guard let interaction: Interaction = try? Interaction.fetchOne(db, id: callInteractionId) else { - return - } - - let updateToMissedIfNeeded: () throws -> () = { - let missedCallInfo: CallMessage.MessageInfo = CallMessage.MessageInfo(state: .missed) + Storage.shared.writeAsync( + updates: { db in + guard let interaction: Interaction = try? Interaction.fetchOne(db, id: callInteractionId) else { + return + } - guard - let infoMessageData: Data = (interaction.body ?? "").data(using: .utf8), - let messageInfo: CallMessage.MessageInfo = try? JSONDecoder().decode( - CallMessage.MessageInfo.self, - from: infoMessageData - ), - messageInfo.state == .incoming, - let missedCallInfoData: Data = try? JSONEncoder().encode(missedCallInfo) - else { return } + let updateToMissedIfNeeded: () throws -> () = { + let missedCallInfo: CallMessage.MessageInfo = CallMessage.MessageInfo(state: .missed) + + guard + let infoMessageData: Data = (interaction.body ?? "").data(using: .utf8), + let messageInfo: CallMessage.MessageInfo = try? JSONDecoder().decode( + CallMessage.MessageInfo.self, + from: infoMessageData + ), + messageInfo.state == .incoming, + let missedCallInfoData: Data = try? JSONEncoder().encode(missedCallInfo) + else { return } + + _ = try interaction + .with(body: String(data: missedCallInfoData, encoding: .utf8)) + .saved(db) + } + let shouldMarkAsRead: Bool = try { + if duration > 0 { return true } + if hasStartedConnecting { return true } + + switch mode { + case .local: + try updateToMissedIfNeeded() + return true + + case .remote, .unanswered: + try updateToMissedIfNeeded() + return false + + case .answeredElsewhere: return true + } + }() - _ = try interaction - .with(body: String(data: missedCallInfoData, encoding: .utf8)) - .saved(db) - } - let shouldMarkAsRead: Bool = try { - if duration > 0 { return true } - if hasStartedConnecting { return true } + guard shouldMarkAsRead else { return } - switch mode { - case .local: - try updateToMissedIfNeeded() - return true - - case .remote, .unanswered: - try updateToMissedIfNeeded() - return false - - case .answeredElsewhere: return true + try Interaction.markAsRead( + db, + interactionId: interaction.id, + threadId: interaction.threadId, + includingOlder: false, + trySendReadReceipt: false + ) + }, + completion: { _, _ in + if CurrentAppContext().isInBackground() { + // Stop all jobs except for message sending and when completed suspend the database + JobRunner.stopAndClearPendingJobs(exceptForVariant: .messageSend) { + NotificationCenter.default.post(name: Database.suspendNotification, object: self) + } } - }() - - guard shouldMarkAsRead else { return } - - try Interaction.markAsRead( - db, - interactionId: interaction.id, - threadId: interaction.threadId, - includingOlder: false, - trySendReadReceipt: false - ) - } + } + ) } // MARK: - Renderer diff --git a/Session/Calls/Call Management/SessionCallManager.swift b/Session/Calls/Call Management/SessionCallManager.swift index e7ab88431..aa7e5a998 100644 --- a/Session/Calls/Call Management/SessionCallManager.swift +++ b/Session/Calls/Call Management/SessionCallManager.swift @@ -136,12 +136,6 @@ public final class SessionCallManager: NSObject, CallManagerProtocol { func handleCallEnded() { WebRTCSession.current = nil UserDefaults.sharedLokiProject?.set(false, forKey: "isCallOngoing") - if CurrentAppContext().isInBackground() { - // Stop all jobs except for message sending and when completed suspend the database - JobRunner.stopAndClearPendingJobs(exceptForVariant: .messageSend) { - NotificationCenter.default.post(name: Database.suspendNotification, object: self) - } - } } guard let call = currentCall else {