Skip to content

Commit

Permalink
wait until dispatched jobs are done
Browse files Browse the repository at this point in the history
  • Loading branch information
r10s committed May 13, 2021
1 parent d355b60 commit 73db6a1
Showing 1 changed file with 18 additions and 3 deletions.
21 changes: 18 additions & 3 deletions deltachat-ios/AppDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -202,9 +202,15 @@ class AppDelegate: UIResponder, UIApplicationDelegate, UNUserNotificationCenterD
} else if app.backgroundTimeRemaining < 10 {
logger.info("⬅️ few background time, \(app.backgroundTimeRemaining), stopping")
self.dcContext.stopIo()
DispatchQueue.main.asyncAfter(deadline: .now() + 3) {

// to avoid 0xdead10cc exceptions, scheduled jobs need to be done before we get suspended;
// we increase the probabilty that this happens by adding unregisterBackgroundTask()
// to the end of both dispatch queues.
DispatchQueue.main.asyncAfter(deadline: .now() + 1) {
logger.info("⬅️ few background time, \(app.backgroundTimeRemaining), done")
self.unregisterBackgroundTask()
DispatchQueue.global(qos: .background).async {
self.unregisterBackgroundTask()
}
}
} else {
logger.info("⬅️ remaining background time: \(app.backgroundTimeRemaining)")
Expand Down Expand Up @@ -382,7 +388,16 @@ class AppDelegate: UIResponder, UIApplicationDelegate, UNUserNotificationCenterD
if !self.appIsInForeground() {
self.dcContext.stopIo()
}
completionHandler(.newData)

// to avoid 0xdead10cc exceptions, scheduled jobs need to be done before we get suspended;
// we increase the probabilty that this happens by adding completionHandler()
// to the end of both dispatch queues.
DispatchQueue.main.asyncAfter(deadline: .now() + 1) {
logger.info("⬅️ fetch finished")
DispatchQueue.global(qos: .background).async {
completionHandler(.newData)
}
}
}
}

Expand Down

0 comments on commit 73db6a1

Please sign in to comment.