Skip to content

Commit

Permalink
Fix #5597: if sync is running, don't schedule a bg task (#5843)
Browse files Browse the repository at this point in the history
  • Loading branch information
garvankeeley authored Dec 2, 2019
1 parent f36fcb4 commit 4876ba5
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 14 deletions.
34 changes: 25 additions & 9 deletions Client/Application/AppDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -374,7 +374,7 @@ class AppDelegate: UIResponder, UIApplicationDelegate, UIViewControllerRestorati
shutdownWebServer = singleShotTimer

if #available(iOS 13.0, *) {
scheduleBGSync()
scheduleBGSync(application: application)
} else {
syncOnDidEnterBackground(application: application)
}
Expand Down Expand Up @@ -549,14 +549,30 @@ class AppDelegate: UIResponder, UIApplicationDelegate, UIViewControllerRestorati
}

@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)
private func scheduleBGSync(application: UIApplication) {
if profile?.syncManager.isSyncing ?? false {
// If syncing, create a bg task because _shutdown() is blocking and might take a few seconds to complete
var taskId = UIBackgroundTaskIdentifier(rawValue: 0)
taskId = application.beginBackgroundTask(expirationHandler: {
application.endBackgroundTask(taskId)
})

DispatchQueue.main.async {
self.profile?._shutdown()
application.endBackgroundTask(taskId)
}
} else {
// Blocking call, however without sync running it should be instantaneous
profile?._shutdown()

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)
}
}
}
}
Expand Down
12 changes: 7 additions & 5 deletions Storage/Rust/RustLogins.swift
Original file line number Diff line number Diff line change
Expand Up @@ -157,16 +157,18 @@ public class RustLogins {
return error
}

public func forceClose() -> NSError? {
var error: NSError?

public func interrupt() {
do {
try storage.interrupt()
} catch let err as NSError {
error = err

Sentry.shared.sendWithStacktrace(message: "Error interrupting Logins database", tag: SentryTag.rustLogins, severity: .error, description: err.localizedDescription)
}
}

public func forceClose() -> NSError? {
var error: NSError?

interrupt()

queue.sync {
guard isOpen else { return }
Expand Down
4 changes: 4 additions & 0 deletions Storage/Rust/RustPlaces.swift
Original file line number Diff line number Diff line change
Expand Up @@ -267,6 +267,10 @@ public class RustPlaces {
return error
}

public func interrupt() {
api?.interrupt()
}

public func forceClose() -> NSError? {
var error: NSError? = nil

Expand Down

0 comments on commit 4876ba5

Please sign in to comment.