Skip to content

Commit

Permalink
Fix #5704 - BGTaskScheduler for sync on iOS 13 (#5824)
Browse files Browse the repository at this point in the history
  • Loading branch information
garvankeeley committed Dec 2, 2019
1 parent f1929ac commit fb7c071
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 2 deletions.
59 changes: 57 additions & 2 deletions Client/Application/AppDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,11 @@ import Sync
import CoreSpotlight
import UserNotifications

#if canImport(BackgroundTasks)
import BackgroundTasks
#endif


private let log = Logger.browserLogger

let LatestAppVersionProfileKey = "latestAppVersion"
Expand Down Expand Up @@ -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
}

Expand Down Expand Up @@ -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)
Expand All @@ -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) {
Expand Down Expand Up @@ -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
Expand Down
6 changes: 6 additions & 0 deletions Client/Info.plist
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,11 @@
<string>$(POCKET_API_KEY)</string>
<key>MozWhatsNewTopic</key>
<string>whats-new-ios-18</string>
<key>BGTaskSchedulerPermittedIdentifiers</key>
<array>
<string>org.mozilla.ios.sync.part1</string>
<string>org.mozilla.ios.sync.part2</string>
</array>
<key>CFBundleDevelopmentRegion</key>
<string>en</string>
<key>CFBundleDisplayName</key>
Expand Down Expand Up @@ -139,6 +144,7 @@
<key>UIBackgroundModes</key>
<array>
<string>audio</string>
<string>processing</string>
<string>remote-notification</string>
</array>
<key>UILaunchStoryboardName</key>
Expand Down

0 comments on commit fb7c071

Please sign in to comment.