Skip to content

Commit

Permalink
avoid simultaneous background fetches, resulting in startIo/maybeNetw…
Browse files Browse the repository at this point in the history
…ork/stopIo being double-called
  • Loading branch information
r10s committed Apr 8, 2021
1 parent b0e719d commit 2fafeee
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 4 deletions.
22 changes: 22 additions & 0 deletions deltachat-ios/AppDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,11 @@ class AppDelegate: UIResponder, UIApplicationDelegate, UNUserNotificationCenterD
var window: UIWindow?
var notifyToken: String?

// `bgFetchTimestamp` is set to last enter-background or last remote- or local-wakeup.
// in the minute after these events, subsequent remote- or local-wakeups are skipped -
// in favor to the chance of being awakened when it makes more sense.
var bgFetchTimestamp: Double = 0.0


// MARK: - app main entry point

Expand Down Expand Up @@ -167,6 +172,7 @@ class AppDelegate: UIResponder, UIApplicationDelegate, UNUserNotificationCenterD
// eg. to complete sending messages out and to react to responses.
private func registerBackgroundTask() {
logger.info("⬅️ registering background task")
bgFetchTimestamp = Double(Date().timeIntervalSince1970)
backgroundTask = UIApplication.shared.beginBackgroundTask { [weak self] in
// usually, the background thread is finished before in maybeStop()
logger.info("⬅️ background expirationHandler called")
Expand Down Expand Up @@ -341,6 +347,22 @@ class AppDelegate: UIResponder, UIApplicationDelegate, UNUserNotificationCenterD
return
}

// from time to time, `didReceiveRemoteNotification` and `performFetchWithCompletionHandler`
// are actually called at the same millisecond.
//
// therefore, if last fetch is less than a minute ago, we skip this call;
// this also lets the completionHandler being called earlier so that we maybe get awakened when it makes more sense.
//
// nb: calling the completion handler with .noData results in less calls overall.
// if at some point we do per-message-push-notifications, we need to tweak this gate.
let nowTimestamp = Double(Date().timeIntervalSince1970)
if nowTimestamp < bgFetchTimestamp + 60 {
logger.info("➡️ fetch was just executed, skipping")
completionHandler(.newData)
return
}
bgFetchTimestamp = nowTimestamp

// we're in background, run IO for a little time
dcContext.maybeStartIo()
dcContext.maybeNetwork()
Expand Down
12 changes: 8 additions & 4 deletions deltachat-ios/Controller/SettingsController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -477,17 +477,21 @@ internal final class SettingsViewController: UITableViewController, ProgressAler
for name in ["notify-remote-launch", "notify-remote-receive", "notify-local-wakeup"] {
let cnt = UserDefaults.standard.integer(forKey: name + "-count")

let startInt = UserDefaults.standard.double(forKey: name + "-start")
let startStr = startInt==0.0 ? "" : " since " + DateUtils.getExtendedRelativeTimeSpanString(timeStamp: startInt)
let startDbl = UserDefaults.standard.double(forKey: name + "-start")
let startStr = startDbl==0.0 ? "" : " since " + DateUtils.getExtendedRelativeTimeSpanString(timeStamp: startDbl)

let timestampInt = UserDefaults.standard.double(forKey: name + "-last")
let timestampStr = timestampInt==0.0 ? "" : ", last " + DateUtils.getExtendedRelativeTimeSpanString(timeStamp: timestampInt)
let timestampDbl = UserDefaults.standard.double(forKey: name + "-last")
let timestampStr = timestampDbl==0.0 ? "" : ", last " + DateUtils.getExtendedRelativeTimeSpanString(timeStamp: timestampDbl)

info += "\(name)=\(cnt)x\(startStr)\(timestampStr)\n"
}

if let appDelegate = UIApplication.shared.delegate as? AppDelegate {
info += "notify-token=\(appDelegate.notifyToken ?? "<unset>")\n"

let timestampDbl = appDelegate.bgFetchTimestamp
let timestampStr = timestampDbl==0.0 ? "<unset>" : DateUtils.getExtendedRelativeTimeSpanString(timeStamp: timestampDbl)
info += "last-bg-fetch=\(timestampStr)\n"
}

#if DEBUG
Expand Down

0 comments on commit 2fafeee

Please sign in to comment.