Skip to content

Commit

Permalink
track durations of the last 16 background fetches
Browse files Browse the repository at this point in the history
  • Loading branch information
r10s committed Feb 19, 2022
1 parent f5ba9b5 commit 7b56b1d
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 2 deletions.
20 changes: 18 additions & 2 deletions deltachat-ios/AppDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -444,19 +444,25 @@ class AppDelegate: UIResponder, UIApplicationDelegate, UNUserNotificationCenterD

DispatchQueue.main.asyncAfter(deadline: .now() + 10) { [weak self] in
logger.info("⬅️ finishing fetch")

guard let self = self else {
completionHandler(.failed)
return
}

if !self.appIsInForeground() {
self.dcAccounts.stopIo()
}

// to avoid 0xdead10cc exceptions, scheduled jobs need to be done before we get suspended;
// we increase the probabilty that this happens by waiting a moment before calling completionHandler()
DispatchQueue.main.asyncAfter(deadline: .now() + 1) {
DispatchQueue.main.asyncAfter(deadline: .now() + 1) { [weak self] in
logger.info("⬅️ fetch done")
guard let self = self else {
completionHandler(.failed)
return
}

self.pushToDebugArray(name: "notify-fetch-durations", value: Double(Date().timeIntervalSince1970)-nowTimestamp)
completionHandler(.newData)

if backgroundTask != .invalid {
Expand Down Expand Up @@ -563,6 +569,16 @@ class AppDelegate: UIResponder, UIApplicationDelegate, UNUserNotificationCenterD
}
}

private func pushToDebugArray(name: String, value: Double) {
let values = UserDefaults.standard.array(forKey: name)
var slidingValues = [Double]()
if values != nil, let values = values as? [Double] {
slidingValues = values.suffix(16)
}
slidingValues.append(value)
UserDefaults.standard.set(slidingValues, forKey: name)
}

private func setStockTranslations() {
let dcContext = dcAccounts.getSelected()
dcContext.setStockTranslation(id: DC_STR_NOMESSAGES, localizationKey: "chat_no_messages")
Expand Down
9 changes: 9 additions & 0 deletions deltachat-ios/Controller/SettingsController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -698,6 +698,15 @@ internal final class SettingsViewController: UITableViewController, ProgressAler
info += "\(name)=\(cnt)x\(startStr)\(timestampStr)\n"
}

info += "notify-fetch-durations="
let fetchDurations = UserDefaults.standard.array(forKey: "notify-fetch-durations") as? [Double]
if let fetchDurations = fetchDurations {
for fetchDuration in fetchDurations {
info += String(format: "%.3fs ", fetchDuration)
}
}
info += "\n"

var val = "?"
switch UIApplication.shared.backgroundRefreshStatus {
case .restricted: val = "restricted"
Expand Down

0 comments on commit 7b56b1d

Please sign in to comment.