diff --git a/submodules/TelegramUI/TelegramUI/AppDelegate.swift b/submodules/TelegramUI/TelegramUI/AppDelegate.swift index 71c746d538..7142fc74d9 100644 --- a/submodules/TelegramUI/TelegramUI/AppDelegate.swift +++ b/submodules/TelegramUI/TelegramUI/AppDelegate.swift @@ -1331,7 +1331,7 @@ final class SharedApplicationContext { #if DEBUG extendNow = false #endif - sharedApplicationContext.wakeupManager.allowBackgroundTimeExtension(timeout: 4.0, extendNow: extendNow) + sharedApplicationContext.wakeupManager.allowBackgroundTimeExtension(timeout: 2.0, extendNow: extendNow) }) self.isInForegroundValue = false @@ -1385,7 +1385,7 @@ final class SharedApplicationContext { let _ = (self.sharedContextPromise.get() |> take(1) |> deliverOnMainQueue).start(next: { sharedApplicationContext in - sharedApplicationContext.wakeupManager.allowBackgroundTimeExtension(timeout: 4.0) + sharedApplicationContext.wakeupManager.allowBackgroundTimeExtension(timeout: 2.0) }) var redactedPayload = userInfo @@ -1507,7 +1507,7 @@ final class SharedApplicationContext { } /*.start(next: { sharedApplicationContext in - sharedApplicationContext.wakeupManager.allowBackgroundTimeExtension(timeout: 4.0) + sharedApplicationContext.wakeupManager.allowBackgroundTimeExtension(timeout: 2.0) if case PKPushType.voIP = type { Logger.shared.log("App \(self.episodeId)", "pushRegistry payload: \(payload.dictionaryPayload)") @@ -1717,7 +1717,7 @@ final class SharedApplicationContext { |> take(1) |> deliverOnMainQueue |> mapToSignal { sharedContext -> Signal in - sharedContext.wakeupManager.allowBackgroundTimeExtension(timeout: 4.0) + sharedContext.wakeupManager.allowBackgroundTimeExtension(timeout: 2.0) return sharedContext.sharedContext.activeAccounts |> mapToSignal { _, accounts, _ -> Signal in for account in accounts { diff --git a/submodules/TelegramUI/TelegramUI/SharedWakeupManager.swift b/submodules/TelegramUI/TelegramUI/SharedWakeupManager.swift index c6e42eb86c..962ac8a234 100644 --- a/submodules/TelegramUI/TelegramUI/SharedWakeupManager.swift +++ b/submodules/TelegramUI/TelegramUI/SharedWakeupManager.swift @@ -55,6 +55,7 @@ public final class SharedWakeupManager { private var hasActiveAudioSession: Bool = false private var activeExplicitExtensionTimer: SwiftSignalKit.Timer? private var allowBackgroundTimeExtensionDeadline: Double? + private var allowBackgroundTimeExtensionDeadlineTimer: SwiftSignalKit.Timer? private var isInBackgroundExtension: Bool = false private var inForegroundDisposable: Disposable? @@ -84,6 +85,8 @@ public final class SharedWakeupManager { if value { strongSelf.activeExplicitExtensionTimer?.invalidate() strongSelf.activeExplicitExtensionTimer = nil + strongSelf.allowBackgroundTimeExtensionDeadlineTimer?.invalidate() + strongSelf.allowBackgroundTimeExtensionDeadlineTimer = nil } strongSelf.checkTasks() }) @@ -186,6 +189,18 @@ public final class SharedWakeupManager { func allowBackgroundTimeExtension(timeout: Double, extendNow: Bool = false) { let shouldCheckTasks = self.allowBackgroundTimeExtensionDeadline == nil self.allowBackgroundTimeExtensionDeadline = CACurrentMediaTime() + timeout + + self.allowBackgroundTimeExtensionDeadlineTimer?.invalidate() + self.allowBackgroundTimeExtensionDeadlineTimer = SwiftSignalKit.Timer(timeout: timeout, repeat: false, completion: { [weak self] in + guard let strongSelf = self else { + return + } + strongSelf.allowBackgroundTimeExtensionDeadlineTimer?.invalidate() + strongSelf.allowBackgroundTimeExtensionDeadlineTimer = nil + strongSelf.checkTasks() + }, queue: .mainQueue()) + self.allowBackgroundTimeExtensionDeadlineTimer?.start() + if extendNow { if self.activeExplicitExtensionTimer == nil { self.activeExplicitExtensionTimer = SwiftSignalKit.Timer(timeout: 20.0, repeat: false, completion: { [weak self] in @@ -324,6 +339,8 @@ public final class SharedWakeupManager { private func updateAccounts(hasTasks: Bool) { if self.inForeground || self.hasActiveAudioSession || self.isInBackgroundExtension || (hasTasks && self.currentExternalCompletion != nil) || self.activeExplicitExtensionTimer != nil { + Logger.shared.log("Wakeup", "enableBeginTransactions: true (active)") + for (account, primary, tasks) in self.accountsAndTasks { account.postbox.setCanBeginTransactions(true) @@ -337,8 +354,13 @@ public final class SharedWakeupManager { account.shouldKeepBackgroundDownloadConnections.set(.single(tasks.backgroundDownloads)) } } else { + var enableBeginTransactions = false + if self.allowBackgroundTimeExtensionDeadlineTimer != nil { + enableBeginTransactions = true + } + Logger.shared.log("Wakeup", "enableBeginTransactions: \(enableBeginTransactions)") for (account, _, _) in self.accountsAndTasks { - account.postbox.setCanBeginTransactions(false) + account.postbox.setCanBeginTransactions(enableBeginTransactions) account.shouldBeServiceTaskMaster.set(.single(.never)) account.shouldKeepOnlinePresence.set(.single(false)) account.shouldKeepBackgroundDownloadConnections.set(.single(false))