move database suspending logic when handling calls in background after writeAsync

pull/687/head
ryanzhao 2 years ago
parent 9419f83f43
commit 7c1d332763

@ -278,56 +278,65 @@ public final class SessionCall: CurrentCallProtocol, WebRTCSessionDelegate {
let duration: TimeInterval = self.duration let duration: TimeInterval = self.duration
let hasStartedConnecting: Bool = self.hasStartedConnecting let hasStartedConnecting: Bool = self.hasStartedConnecting
// Don't know whether there will be an impact if we don't write async Storage.shared.writeAsync(
Storage.shared.write { db in updates: { db in
guard let interaction: Interaction = try? Interaction.fetchOne(db, id: callInteractionId) else { guard let interaction: Interaction = try? Interaction.fetchOne(db, id: callInteractionId) else {
return return
} }
let updateToMissedIfNeeded: () throws -> () = {
let missedCallInfo: CallMessage.MessageInfo = CallMessage.MessageInfo(state: .missed)
guard let updateToMissedIfNeeded: () throws -> () = {
let infoMessageData: Data = (interaction.body ?? "").data(using: .utf8), let missedCallInfo: CallMessage.MessageInfo = CallMessage.MessageInfo(state: .missed)
let messageInfo: CallMessage.MessageInfo = try? JSONDecoder().decode(
CallMessage.MessageInfo.self, guard
from: infoMessageData let infoMessageData: Data = (interaction.body ?? "").data(using: .utf8),
), let messageInfo: CallMessage.MessageInfo = try? JSONDecoder().decode(
messageInfo.state == .incoming, CallMessage.MessageInfo.self,
let missedCallInfoData: Data = try? JSONEncoder().encode(missedCallInfo) from: infoMessageData
else { return } ),
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 guard shouldMarkAsRead else { return }
.with(body: String(data: missedCallInfoData, encoding: .utf8))
.saved(db)
}
let shouldMarkAsRead: Bool = try {
if duration > 0 { return true }
if hasStartedConnecting { return true }
switch mode { try Interaction.markAsRead(
case .local: db,
try updateToMissedIfNeeded() interactionId: interaction.id,
return true threadId: interaction.threadId,
includingOlder: false,
case .remote, .unanswered: trySendReadReceipt: false
try updateToMissedIfNeeded() )
return false },
completion: { _, _ in
case .answeredElsewhere: return true 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 // MARK: - Renderer

@ -136,12 +136,6 @@ public final class SessionCallManager: NSObject, CallManagerProtocol {
func handleCallEnded() { func handleCallEnded() {
WebRTCSession.current = nil WebRTCSession.current = nil
UserDefaults.sharedLokiProject?.set(false, forKey: "isCallOngoing") 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 { guard let call = currentCall else {

Loading…
Cancel
Save