Use main thread operation to only allow one remote notification at a time to run.

This commit is contained in:
Maurice Parker 2020-04-29 15:46:29 -05:00
parent f193b6da1a
commit d30987ca0a
1 changed files with 32 additions and 8 deletions

View File

@ -24,6 +24,7 @@ class AppDelegate: UIResponder, UIApplicationDelegate, UNUserNotificationCenterD
private var syncBackgroundUpdateTask = UIBackgroundTaskIdentifier.invalid
var syncTimer: ArticleStatusSyncTimer?
private let remoteNotificationoperationQueue = MainThreadOperationQueue()
var shuttingDown = false {
didSet {
@ -111,14 +112,9 @@ class AppDelegate: UIResponder, UIApplicationDelegate, UNUserNotificationCenterD
}
func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable : Any], fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void) {
DispatchQueue.main.async {
self.resumeDatabaseProcessingIfNecessary()
AccountManager.shared.receiveRemoteNotification(userInfo: userInfo) {
self.suspendApplication()
completionHandler(.newData)
}
}
func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable : Any], fetchCompletionHandler completion: @escaping (UIBackgroundFetchResult) -> Void) {
let op = RemoteNotificationOperation(userInfo: userInfo, completion: completion)
remoteNotificationoperationQueue.add(op)
}
func applicationWillTerminate(_ application: UIApplication) {
@ -395,3 +391,31 @@ private extension AppDelegate {
}
}
class RemoteNotificationOperation: MainThreadOperation {
// MainThreadOperation
public var isCanceled = false
public var id: Int?
public weak var operationDelegate: MainThreadOperationDelegate?
public var name: String? = "RemoteNotificationOperation"
public var completionBlock: MainThreadOperation.MainThreadOperationCompletionBlock?
private var userInfo: [AnyHashable : Any]
private var completion: (UIBackgroundFetchResult) -> Void
init(userInfo: [AnyHashable : Any], completion: @escaping (UIBackgroundFetchResult) -> Void) {
self.userInfo = userInfo
self.completion = completion
}
func run() {
appDelegate.resumeDatabaseProcessingIfNecessary()
AccountManager.shared.receiveRemoteNotification(userInfo: userInfo) {
appDelegate.suspendApplication()
self.completion(.newData)
self.operationDelegate?.operationDidComplete(self)
}
}
}