mirror of
https://github.com/simplex-chat/simplex-chat.git
synced 2026-10-22 17:31:55 +00:00
ios: fix background refresh (#769)
* ios: fix background refresh * change app inactive check
This commit is contained in:
1 parent
a8ba4ede82
commit
29c6d51e6a
5 files changed
+23
-18
No files matched your search
@@ -61,17 +61,12 @@ class AppDelegate: NSObject, UIApplicationDelegate {
|
||||
completionHandler(.noData)
|
||||
}
|
||||
} else if let checkMessages = ntfData["checkMessages"] as? Bool, checkMessages {
|
||||
// TODO check if app in background
|
||||
logger.debug("AppDelegate: didReceiveRemoteNotification: checkMessages")
|
||||
// TODO remove
|
||||
// NtfManager.shared.notifyCheckingMessages()
|
||||
completionHandler(.noData)
|
||||
// receiveMessages(completionHandler)
|
||||
} else if let smpQueue = ntfData["checkMessage"] as? String {
|
||||
// TODO check if app in background
|
||||
logger.debug("AppDelegate: didReceiveRemoteNotification: checkMessage \(smpQueue)")
|
||||
completionHandler(.noData)
|
||||
// receiveMessages(completionHandler)
|
||||
if isAppInactive() {
|
||||
receiveMessages(completionHandler)
|
||||
} else {
|
||||
completionHandler(.noData)
|
||||
}
|
||||
} else {
|
||||
completionHandler(.noData)
|
||||
}
|
||||
|
||||
@@ -47,11 +47,16 @@ class BGManager {
|
||||
private func handleRefresh(_ task: BGAppRefreshTask) {
|
||||
logger.debug("BGManager.handleRefresh")
|
||||
schedule()
|
||||
let completeRefresh = completionHandler {
|
||||
if isAppInactive() {
|
||||
let completeRefresh = completionHandler {
|
||||
task.setTaskCompleted(success: true)
|
||||
}
|
||||
task.expirationHandler = { completeRefresh("expirationHandler") }
|
||||
receiveMessages(completeRefresh)
|
||||
} else {
|
||||
logger.debug("BGManager.completionHandler: already active, not started")
|
||||
task.setTaskCompleted(success: true)
|
||||
}
|
||||
task.expirationHandler = { completeRefresh("expirationHandler") }
|
||||
receiveMessages(completeRefresh)
|
||||
}
|
||||
|
||||
func completionHandler(_ complete: @escaping () -> Void) -> ((String) -> Void) {
|
||||
|
||||
@@ -564,8 +564,7 @@ class ChatReceiver {
|
||||
await processReceivedMsg(msg)
|
||||
}
|
||||
if self.receiveMessages {
|
||||
do { try await Task.sleep(nanoseconds: 7_500_000) }
|
||||
catch { logger.error("receiveMsgLoop: Task.sleep error: \(error.localizedDescription)") }
|
||||
_ = try? await Task.sleep(nanoseconds: 7_500_000)
|
||||
await receiveMsgLoop()
|
||||
}
|
||||
}
|
||||
|
||||
@@ -7,6 +7,7 @@
|
||||
//
|
||||
|
||||
import Foundation
|
||||
import UIKit
|
||||
import SimpleXChat
|
||||
|
||||
private let suspendLockQueue = DispatchQueue(label: "chat.simplex.app.suspend.lock")
|
||||
@@ -37,9 +38,13 @@ func suspendBgRefresh() {
|
||||
}
|
||||
|
||||
func chatSuspended() {
|
||||
logger.debug("chatSuspended")
|
||||
suspendLockQueue.sync {
|
||||
if case .suspending = appStateGroupDefault.get() {
|
||||
appStateGroupDefault.set(.suspended)
|
||||
if ChatModel.shared.chatRunning == true {
|
||||
ChatReceiver.shared.stop()
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -50,3 +55,7 @@ func activateChat(appState: AppState = .active) {
|
||||
apiActivateChat()
|
||||
}
|
||||
}
|
||||
|
||||
func isAppInactive() -> Bool {
|
||||
UIApplication.shared.applicationState != .active && appStateGroupDefault.get().inactive
|
||||
}
|
||||
@@ -51,9 +51,6 @@ struct SimpleXApp: App {
|
||||
switch (phase) {
|
||||
case .background:
|
||||
suspendChat()
|
||||
if chatModel.chatRunning == true {
|
||||
ChatReceiver.shared.stop()
|
||||
}
|
||||
BGManager.shared.schedule()
|
||||
if userAuthorized == true {
|
||||
enteredBackground = ProcessInfo.processInfo.systemUptime
|
||||
|
||||
Reference in new issue
Block a user