Fix wakeup

This commit is contained in:
Peter 2019-10-29 00:13:34 +04:00
parent d2e57174a7
commit 95964c993a
2 changed files with 27 additions and 5 deletions

View File

@ -1331,7 +1331,7 @@ final class SharedApplicationContext {
#if DEBUG #if DEBUG
extendNow = false extendNow = false
#endif #endif
sharedApplicationContext.wakeupManager.allowBackgroundTimeExtension(timeout: 4.0, extendNow: extendNow) sharedApplicationContext.wakeupManager.allowBackgroundTimeExtension(timeout: 2.0, extendNow: extendNow)
}) })
self.isInForegroundValue = false self.isInForegroundValue = false
@ -1385,7 +1385,7 @@ final class SharedApplicationContext {
let _ = (self.sharedContextPromise.get() let _ = (self.sharedContextPromise.get()
|> take(1) |> take(1)
|> deliverOnMainQueue).start(next: { sharedApplicationContext in |> deliverOnMainQueue).start(next: { sharedApplicationContext in
sharedApplicationContext.wakeupManager.allowBackgroundTimeExtension(timeout: 4.0) sharedApplicationContext.wakeupManager.allowBackgroundTimeExtension(timeout: 2.0)
}) })
var redactedPayload = userInfo var redactedPayload = userInfo
@ -1507,7 +1507,7 @@ final class SharedApplicationContext {
} }
/*.start(next: { sharedApplicationContext in /*.start(next: { sharedApplicationContext in
sharedApplicationContext.wakeupManager.allowBackgroundTimeExtension(timeout: 4.0) sharedApplicationContext.wakeupManager.allowBackgroundTimeExtension(timeout: 2.0)
if case PKPushType.voIP = type { if case PKPushType.voIP = type {
Logger.shared.log("App \(self.episodeId)", "pushRegistry payload: \(payload.dictionaryPayload)") Logger.shared.log("App \(self.episodeId)", "pushRegistry payload: \(payload.dictionaryPayload)")
@ -1717,7 +1717,7 @@ final class SharedApplicationContext {
|> take(1) |> take(1)
|> deliverOnMainQueue |> deliverOnMainQueue
|> mapToSignal { sharedContext -> Signal<Void, NoError> in |> mapToSignal { sharedContext -> Signal<Void, NoError> in
sharedContext.wakeupManager.allowBackgroundTimeExtension(timeout: 4.0) sharedContext.wakeupManager.allowBackgroundTimeExtension(timeout: 2.0)
return sharedContext.sharedContext.activeAccounts return sharedContext.sharedContext.activeAccounts
|> mapToSignal { _, accounts, _ -> Signal<Account, NoError> in |> mapToSignal { _, accounts, _ -> Signal<Account, NoError> in
for account in accounts { for account in accounts {

View File

@ -55,6 +55,7 @@ public final class SharedWakeupManager {
private var hasActiveAudioSession: Bool = false private var hasActiveAudioSession: Bool = false
private var activeExplicitExtensionTimer: SwiftSignalKit.Timer? private var activeExplicitExtensionTimer: SwiftSignalKit.Timer?
private var allowBackgroundTimeExtensionDeadline: Double? private var allowBackgroundTimeExtensionDeadline: Double?
private var allowBackgroundTimeExtensionDeadlineTimer: SwiftSignalKit.Timer?
private var isInBackgroundExtension: Bool = false private var isInBackgroundExtension: Bool = false
private var inForegroundDisposable: Disposable? private var inForegroundDisposable: Disposable?
@ -84,6 +85,8 @@ public final class SharedWakeupManager {
if value { if value {
strongSelf.activeExplicitExtensionTimer?.invalidate() strongSelf.activeExplicitExtensionTimer?.invalidate()
strongSelf.activeExplicitExtensionTimer = nil strongSelf.activeExplicitExtensionTimer = nil
strongSelf.allowBackgroundTimeExtensionDeadlineTimer?.invalidate()
strongSelf.allowBackgroundTimeExtensionDeadlineTimer = nil
} }
strongSelf.checkTasks() strongSelf.checkTasks()
}) })
@ -186,6 +189,18 @@ public final class SharedWakeupManager {
func allowBackgroundTimeExtension(timeout: Double, extendNow: Bool = false) { func allowBackgroundTimeExtension(timeout: Double, extendNow: Bool = false) {
let shouldCheckTasks = self.allowBackgroundTimeExtensionDeadline == nil let shouldCheckTasks = self.allowBackgroundTimeExtensionDeadline == nil
self.allowBackgroundTimeExtensionDeadline = CACurrentMediaTime() + timeout 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 extendNow {
if self.activeExplicitExtensionTimer == nil { if self.activeExplicitExtensionTimer == nil {
self.activeExplicitExtensionTimer = SwiftSignalKit.Timer(timeout: 20.0, repeat: false, completion: { [weak self] in 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) { private func updateAccounts(hasTasks: Bool) {
if self.inForeground || self.hasActiveAudioSession || self.isInBackgroundExtension || (hasTasks && self.currentExternalCompletion != nil) || self.activeExplicitExtensionTimer != nil { 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 { for (account, primary, tasks) in self.accountsAndTasks {
account.postbox.setCanBeginTransactions(true) account.postbox.setCanBeginTransactions(true)
@ -337,8 +354,13 @@ public final class SharedWakeupManager {
account.shouldKeepBackgroundDownloadConnections.set(.single(tasks.backgroundDownloads)) account.shouldKeepBackgroundDownloadConnections.set(.single(tasks.backgroundDownloads))
} }
} else { } else {
var enableBeginTransactions = false
if self.allowBackgroundTimeExtensionDeadlineTimer != nil {
enableBeginTransactions = true
}
Logger.shared.log("Wakeup", "enableBeginTransactions: \(enableBeginTransactions)")
for (account, _, _) in self.accountsAndTasks { for (account, _, _) in self.accountsAndTasks {
account.postbox.setCanBeginTransactions(false) account.postbox.setCanBeginTransactions(enableBeginTransactions)
account.shouldBeServiceTaskMaster.set(.single(.never)) account.shouldBeServiceTaskMaster.set(.single(.never))
account.shouldKeepOnlinePresence.set(.single(false)) account.shouldKeepOnlinePresence.set(.single(false))
account.shouldKeepBackgroundDownloadConnections.set(.single(false)) account.shouldKeepBackgroundDownloadConnections.set(.single(false))