From 6cc3eb57a1f2a4ef110c59cda4b44b26e5408bcc Mon Sep 17 00:00:00 2001 From: Oliver Wiese <oliver.wiese@fu-berlin.de> Date: Tue, 30 Oct 2018 11:19:06 +0100 Subject: [PATCH] finish background fetch add logging --- enzevalos_iphone/AppDelegate.swift | 37 ++++++++++---------- enzevalos_iphone/DataHandler.swift | 2 +- enzevalos_iphone/Logger.swift | 13 +++++++ enzevalos_iphone/LoggingEventType.swift | 3 +- enzevalos_iphone/MailHandler.swift | 21 +++++------ enzevalos_iphone/PLists/enzevalos-Info.plist | 4 +-- 6 files changed, 48 insertions(+), 32 deletions(-) diff --git a/enzevalos_iphone/AppDelegate.swift b/enzevalos_iphone/AppDelegate.swift index e11e0291..d033e994 100644 --- a/enzevalos_iphone/AppDelegate.swift +++ b/enzevalos_iphone/AppDelegate.swift @@ -33,6 +33,8 @@ class AppDelegate: UIResponder, UIApplicationDelegate { var mailHandler = MailHandler() var orientationLock = UIInterfaceOrientationMask.allButUpsideDown var counterBackgroundFetch = 0 + var start: Date = Date() + func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool { // Override point for customization after application launch. @@ -77,10 +79,11 @@ class AppDelegate: UIResponder, UIApplicationDelegate { if #available(iOS 11.0, *) { QAKit.Fingertips.start() } - // Set background fetching time interval. - let backgroundFetchInterval : Double = 10 //60*5 // seconds * minutes or UIApplicationBackgroundFetchIntervalMinimum + // Set background fetching time interval to 5 min + // Alternative: UIApplicationBackgroundFetchIntervalMinimum + let backgroundFetchInterval : Double = 60*5 // = seconds * minutes UIApplication.shared.setMinimumBackgroundFetchInterval(backgroundFetchInterval) - + return true } @@ -228,9 +231,7 @@ class AppDelegate: UIResponder, UIApplicationDelegate { } func onboardingDone() { -// Logger.queue.async(flags: .barrier) { - Logger.log(onboardingState: "done") -// } + Logger.log(onboardingState: "done") UserDefaults.standard.set(true, forKey: "launchedBefore") self.window?.rootViewController = UIStoryboard(name: "Main", bundle: nil).instantiateInitialViewController() presentInboxViewController() @@ -239,9 +240,8 @@ class AppDelegate: UIResponder, UIApplicationDelegate { func applicationWillResignActive(_ application: UIApplication) { // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state. // Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game. -// Logger.queue.async(flags: .barrier) { - Logger.log(background: true) -// } + UIApplication.shared.applicationIconBadgeNumber = 0 + Logger.log(background: true) } func applicationDidEnterBackground(_ application: UIApplication) { @@ -347,24 +347,25 @@ class AppDelegate: UIResponder, UIApplicationDelegate { } - // Support for background fetch func application(_ application: UIApplication, performFetchWithCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void) { counterBackgroundFetch = counterBackgroundFetch + 1 - print("Fetch mails! #Fetches", counterBackgroundFetch ) - mailHandler.newMails(completionCallback: hasNewMails(_:performFetchWithCompletionHandler: ), performFetchWithCompletionHandler: completionHandler) + start = Date() + mailHandler.backgroundUpdate(completionCallback: hasNewMails(_:performFetchWithCompletionHandler: ), performFetchWithCompletionHandler: completionHandler) } - var c = 1 func hasNewMails(_ newMails: UInt32, performFetchWithCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void){ - print("Have we new Mails?: ", newMails) - c = c + 1 - UIApplication.shared.applicationIconBadgeNumber = Int(c) + let duration = Date().timeIntervalSince(start) + Logger.log(backgroundFetch: newMails, duration: duration) if newMails > 0 { - // UIApplication.shared.applicationIconBadgeNumber = Int(newMails) + UIApplication.shared.applicationIconBadgeNumber = Int(newMails) + completionHandler(.newData) + } else { + UIApplication.shared.applicationIconBadgeNumber = 0 + completionHandler(.noData) } - completionHandler(.newData) + } } diff --git a/enzevalos_iphone/DataHandler.swift b/enzevalos_iphone/DataHandler.swift index 756948f0..424806fd 100644 --- a/enzevalos_iphone/DataHandler.swift +++ b/enzevalos_iphone/DataHandler.swift @@ -777,7 +777,7 @@ class DataHandler { myfolder.addToMails(mail) if mail.uid > myfolder.maxID { myfolder.maxID = mail.uid - print("New ID: %s", myfolder.maxID) + print("New ID: \( myfolder.maxID)") } var record = getKeyRecord(addr: mail.from.mailAddress, keyID: nil) if let signedID = mail.signedKey?.keyID { diff --git a/enzevalos_iphone/Logger.swift b/enzevalos_iphone/Logger.swift index f78f3c1f..fd133142 100644 --- a/enzevalos_iphone/Logger.swift +++ b/enzevalos_iphone/Logger.swift @@ -625,6 +625,19 @@ class Logger { sendCheck() } + static func log(backgroundFetch newMails: UInt32, duration: Double) { + if !logging { + return + } + + var event = plainLogDict() + event["type"] = LoggingEventType.backgroundFetch.rawValue + event["newMails"] = Int(newMails) + event["duration"] = duration + + saveToDisk(json: dictToJSON(fields: event)) + } + static func log(verify keyID: String, open: Bool, success: Bool? = nil) { if !logging { return diff --git a/enzevalos_iphone/LoggingEventType.swift b/enzevalos_iphone/LoggingEventType.swift index c2ded63d..271b1b7d 100644 --- a/enzevalos_iphone/LoggingEventType.swift +++ b/enzevalos_iphone/LoggingEventType.swift @@ -61,5 +61,6 @@ enum LoggingEventType: String { search = "search", gotBitcoinMail = "gotBitcoinMail", onboardingPageTransition = "onboardingPageTransition", - onboardingState = "onboardingState" + onboardingState = "onboardingState", + backgroundFetch = "backgroundFetch" } diff --git a/enzevalos_iphone/MailHandler.swift b/enzevalos_iphone/MailHandler.swift index cb18ab92..c53fb082 100644 --- a/enzevalos_iphone/MailHandler.swift +++ b/enzevalos_iphone/MailHandler.swift @@ -793,20 +793,17 @@ class MailHandler { } } - func newMails(completionCallback: @escaping (_ newMails: UInt32, _ completionHandler: @escaping (UIBackgroundFetchResult) -> Void) -> (), performFetchWithCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void){ + func backgroundUpdate(completionCallback: @escaping (_ newMails: UInt32, _ completionHandler: @escaping (UIBackgroundFetchResult) -> Void) -> (), performFetchWithCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void){ let folder = DataHandler.handler.findFolder(with: INBOX) let folderstatus = IMAPSession.folderStatusOperation(folder.path) - print("Ask folder") + // Work only in background thread.... var backgroundTaskID: Int? DispatchQueue.global(qos: .background).async { - print("This is run on the background queue") backgroundTaskID = UIApplication.shared.beginBackgroundTask (withName: "Finish Network Tasks"){ UIApplication.shared.endBackgroundTask(backgroundTaskID!) backgroundTaskID = UIBackgroundTaskInvalid } - folderstatus?.start { (error, status) -> Void in - print("Result!") guard error == nil else { UIApplication.shared.endBackgroundTask(backgroundTaskID!) backgroundTaskID = UIBackgroundTaskInvalid @@ -816,11 +813,15 @@ class MailHandler { if let status = status { let uidValidity = status.uidValidity let uid = status.uidNext - let newMails = status.recentCount - print("Status: ", status) - print("newMails: ", newMails) - let currentDateTime = Date() - print(currentDateTime, " Folder maxID: ", folder.maxID) + var newMails: UInt32 = 0 + var diff = uid.subtractingReportingOverflow(UInt32(folder.maxID)) + diff = diff.partialValue.subtractingReportingOverflow(1) + if diff.overflow { + newMails = 0 + } + else { + newMails = diff.partialValue + } if (uidValidity != folder.uidvalidity || folder.maxID < uid - 1) { UIApplication.shared.endBackgroundTask(backgroundTaskID!) backgroundTaskID = UIBackgroundTaskInvalid diff --git a/enzevalos_iphone/PLists/enzevalos-Info.plist b/enzevalos_iphone/PLists/enzevalos-Info.plist index 3b486a0a..5e517789 100644 --- a/enzevalos_iphone/PLists/enzevalos-Info.plist +++ b/enzevalos_iphone/PLists/enzevalos-Info.plist @@ -17,7 +17,7 @@ <key>CFBundlePackageType</key> <string>APPL</string> <key>CFBundleShortVersionString</key> - <string>0.8.7</string> + <string>0.8.8</string> <key>CFBundleSignature</key> <string>????</string> <key>CFBundleURLTypes</key> @@ -32,7 +32,7 @@ </dict> </array> <key>CFBundleVersion</key> - <string>0.8.7</string> + <string>0.8.8</string> <key>LSRequiresIPhoneOS</key> <true/> <key>NSAppTransportSecurity</key> -- GitLab