Convert receiveRemoteNotification to async/await.

This commit is contained in:
Brent Simmons
2023-10-10 22:07:04 -07:00
parent 378e116b5c
commit 9285a956fa
11 changed files with 35 additions and 41 deletions

View File

@@ -424,8 +424,8 @@ public enum FetchType {
grantingType.requestOAuthAccessToken(with: response, transport: transport, completion: completion)
}
public func receiveRemoteNotification(userInfo: [AnyHashable : Any], completion: @escaping () -> Void) {
delegate.receiveRemoteNotification(for: self, userInfo: userInfo, completion: completion)
public func receiveRemoteNotification(userInfo: [AnyHashable : Any]) async {
await delegate.receiveRemoteNotification(for: self, userInfo: userInfo)
}
public func refreshAll(completion: @escaping (Result<Void, Error>) -> Void) {

View File

@@ -23,7 +23,7 @@ import Secrets
var refreshProgress: DownloadProgress { get }
func receiveRemoteNotification(for account: Account, userInfo: [AnyHashable : Any], completion: @escaping () -> Void)
func receiveRemoteNotification(for account: Account, userInfo: [AnyHashable : Any]) async
func refreshAll(for account: Account, completion: @escaping (Result<Void, Error>) -> Void)
func syncArticleStatus(for account: Account, completion: ((Result<Void, Error>) -> Void)?)

View File

@@ -71,8 +71,8 @@ public enum FeedbinAccountDelegateError: String, Error {
self.caller.delegate = self
}
func receiveRemoteNotification(for account: Account, userInfo: [AnyHashable : Any], completion: @escaping () -> Void) {
completion()
func receiveRemoteNotification(for account: Account, userInfo: [AnyHashable : Any]) async {
return
}
func refreshAll(for account: Account, completion: @escaping (Result<Void, Error>) -> Void) {

View File

@@ -40,10 +40,10 @@ final class LocalAccountDelegate: AccountDelegate, Logging {
let refreshProgress = DownloadProgress(numberOfTasks: 0)
func receiveRemoteNotification(for account: Account, userInfo: [AnyHashable : Any], completion: @escaping () -> Void) {
completion()
func receiveRemoteNotification(for account: Account, userInfo: [AnyHashable : Any]) async {
return
}
func refreshAll(for account: Account, completion: @escaping (Result<Void, Error>) -> Void) {
guard refreshProgress.isComplete else {
completion(.success(()))

View File

@@ -62,10 +62,10 @@ final class NewsBlurAccountDelegate: AccountDelegate, Logging {
database = SyncDatabase(databaseFilePath: dataFolder.appending("/DB.sqlite3"))
}
func receiveRemoteNotification(for account: Account, userInfo: [AnyHashable : Any], completion: @escaping () -> Void) {
completion()
func receiveRemoteNotification(for account: Account, userInfo: [AnyHashable : Any]) async {
return
}
func refreshAll(for account: Account, completion: @escaping (Result<Void, Error>) -> ()) {
self.refreshProgress.addToNumberOfTasksAndRemaining(4)

View File

@@ -99,10 +99,10 @@ public enum ReaderAPIAccountDelegateError: LocalizedError {
self.caller.variant = variant
}
func receiveRemoteNotification(for account: Account, userInfo: [AnyHashable : Any], completion: @escaping () -> Void) {
completion()
func receiveRemoteNotification(for account: Account, userInfo: [AnyHashable : Any]) async {
return
}
func refreshAll(for account: Account, completion: @escaping (Result<Void, Error>) -> Void) {
refreshProgress.addToNumberOfTasksAndRemaining(6)

View File

@@ -239,18 +239,9 @@ import RSDatabase
}
}
public func receiveRemoteNotification(userInfo: [AnyHashable : Any], completion: (() -> Void)? = nil) {
let group = DispatchGroup()
public func receiveRemoteNotification(userInfo: [AnyHashable : Any]) async {
for account in activeAccounts {
group.enter()
account.receiveRemoteNotification(userInfo: userInfo) {
group.leave()
}
}
group.notify(queue: DispatchQueue.main) {
completion?()
await account.receiveRemoteNotification(userInfo: userInfo)
}
}

View File

@@ -68,14 +68,16 @@ final class CloudKitAccountDelegate: AccountDelegate, Logging {
database = SyncDatabase(databaseFilePath: databaseFilePath)
}
func receiveRemoteNotification(for account: Account, userInfo: [AnyHashable : Any], completion: @escaping () -> Void) {
let op = CloudKitRemoteNotificationOperation(accountZone: accountZone, articlesZone: articlesZone, userInfo: userInfo)
op.completionBlock = { mainThreadOperaion in
completion()
func receiveRemoteNotification(for account: Account, userInfo: [AnyHashable : Any]) async {
await withCheckedContinuation { continuation in
let op = CloudKitRemoteNotificationOperation(accountZone: accountZone, articlesZone: articlesZone, userInfo: userInfo)
op.completionBlock = { mainThreadOperation in
continuation.resume()
}
mainThreadOperationQueue.add(op)
}
mainThreadOperationQueue.add(op)
}
func refreshAll(for account: Account, completion: @escaping (Result<Void, Error>) -> Void) {
guard refreshProgress.isComplete else {
completion(.success(()))

View File

@@ -103,8 +103,8 @@ final class FeedlyAccountDelegate: AccountDelegate, Logging {
// MARK: Account API
func receiveRemoteNotification(for account: Account, userInfo: [AnyHashable : Any], completion: @escaping () -> Void) {
completion()
func receiveRemoteNotification(for account: Account, userInfo: [AnyHashable : Any]) async {
return
}
func refreshAll(for account: Account, completion: @escaping (Result<Void, Error>) -> Void) {

View File

@@ -318,7 +318,9 @@ var appDelegate: AppDelegate!
}
func application(_ application: NSApplication, didReceiveRemoteNotification userInfo: [String : Any]) {
AccountManager.shared.receiveRemoteNotification(userInfo: userInfo)
Task { @MainActor in
await AccountManager.shared.receiveRemoteNotification(userInfo: userInfo)
}
}
func application(_ sender: NSApplication, openFile filename: String) -> Bool {

View File

@@ -124,15 +124,14 @@ var appDelegate: AppDelegate!
}
func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable : Any], fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void) {
DispatchQueue.main.async {
Task { @MainActor in
self.resumeDatabaseProcessingIfNecessary()
AccountManager.shared.receiveRemoteNotification(userInfo: userInfo) {
self.suspendApplication()
completionHandler(.newData)
}
await AccountManager.shared.receiveRemoteNotification(userInfo: userInfo)
self.suspendApplication()
completionHandler(.newData)
}
}
}
func applicationWillTerminate(_ application: UIApplication) {
shuttingDown = true
}