From fb7c07164fc0efe58b9a48791e96976563606eda Mon Sep 17 00:00:00 2001 From: Garvan Keeley Date: Fri, 29 Nov 2019 06:36:22 -0800 Subject: [PATCH] Fix #5704 - BGTaskScheduler for sync on iOS 13 (#5824) --- Client/Application/AppDelegate.swift | 59 +++++++++++++++++++++++++++- Client/Info.plist | 6 +++ 2 files changed, 63 insertions(+), 2 deletions(-) diff --git a/Client/Application/AppDelegate.swift b/Client/Application/AppDelegate.swift index 89b7ed19c97e..984d5beb7565 100644 --- a/Client/Application/AppDelegate.swift +++ b/Client/Application/AppDelegate.swift @@ -15,6 +15,11 @@ import Sync import CoreSpotlight import UserNotifications +#if canImport(BackgroundTasks) + import BackgroundTasks +#endif + + private let log = Logger.browserLogger let LatestAppVersionProfileKey = "latestAppVersion" @@ -226,6 +231,40 @@ class AppDelegate: UIResponder, UIApplicationDelegate, UIViewControllerRestorati LeanPlumClient.shared.set(enabled: true) } + if #available(iOS 13.0, *) { + BGTaskScheduler.shared.register(forTaskWithIdentifier: "org.mozilla.ios.sync.part1", using: DispatchQueue.global()) { task in + guard self.profile?.hasSyncableAccount() ?? false else { + self.shutdownProfileWhenNotActive(application) + return + } + + NSLog("background sync part 1") // NSLog to see in device console + let collection = ["bookmarks", "history"] + self.profile?.syncManager.syncNamedCollections(why: .backgrounded, names: collection).uponQueue(.main) { _ in + task.setTaskCompleted(success: true) + let request = BGProcessingTaskRequest(identifier: "org.mozilla.ios.sync.part2") + request.earliestBeginDate = Date(timeIntervalSinceNow: 1) + request.requiresNetworkConnectivity = true + do { + try BGTaskScheduler.shared.submit(request) + } catch { + NSLog(error.localizedDescription) + } + } + } + + // Split up the sync tasks so each can get maximal time for a bg task. + // This task runs after the bookmarks+history sync. + BGTaskScheduler.shared.register(forTaskWithIdentifier: "org.mozilla.ios.sync.part2", using: DispatchQueue.global()) { task in + NSLog("background sync part 2") // NSLog to see in device console + let collection = ["tabs", "logins", "clients"] + self.profile?.syncManager.syncNamedCollections(why: .backgrounded, names: collection).uponQueue(.main) { _ in + self.shutdownProfileWhenNotActive(application) + task.setTaskCompleted(success: true) + } + } + } + return shouldPerformAdditionalDelegateHandling } @@ -322,8 +361,6 @@ class AppDelegate: UIResponder, UIApplicationDelegate, UIViewControllerRestorati // TODO: iOS 13 needs to iterate all the BVCs. BrowserViewController.foregroundBVC().downloadQueue.pauseAll() - syncOnDidEnterBackground(application: application) - UnifiedTelemetry.recordEvent(category: .action, method: .background, object: .app) let singleShotTimer = DispatchSource.makeTimerSource(queue: DispatchQueue.main) @@ -335,6 +372,12 @@ class AppDelegate: UIResponder, UIApplicationDelegate, UIViewControllerRestorati } singleShotTimer.resume() shutdownWebServer = singleShotTimer + + if #available(iOS 13.0, *) { + scheduleBGSync() + } else { + syncOnDidEnterBackground(application: application) + } } fileprivate func syncOnDidEnterBackground(application: UIApplication) { @@ -504,6 +547,18 @@ class AppDelegate: UIResponder, UIApplicationDelegate, UIViewControllerRestorati completionHandler(handledShortCutItem) } + + @available(iOS 13.0, *) + private func scheduleBGSync() { + let request = BGProcessingTaskRequest(identifier: "org.mozilla.ios.sync.part1") + request.earliestBeginDate = Date(timeIntervalSinceNow: 1) + request.requiresNetworkConnectivity = true + do { + try BGTaskScheduler.shared.submit(request) + } catch { + NSLog(error.localizedDescription) + } + } } // MARK: - Root View Controller Animations diff --git a/Client/Info.plist b/Client/Info.plist index 172f20650799..844a9bae8c9a 100644 --- a/Client/Info.plist +++ b/Client/Info.plist @@ -20,6 +20,11 @@ $(POCKET_API_KEY) MozWhatsNewTopic whats-new-ios-18 + BGTaskSchedulerPermittedIdentifiers + + org.mozilla.ios.sync.part1 + org.mozilla.ios.sync.part2 + CFBundleDevelopmentRegion en CFBundleDisplayName @@ -139,6 +144,7 @@ UIBackgroundModes audio + processing remote-notification UILaunchStoryboardName