From dbee19e16976fca2c61af021256f025a43142891 Mon Sep 17 00:00:00 2001 From: Brent Simmons Date: Sun, 24 Mar 2024 21:52:30 -0700 Subject: [PATCH] Make most of MainThreadOperation @MainActor. --- .../CloudKit/CloudKitAccountDelegate.swift | 20 +++++++++++-------- Core/Sources/Core/MainThreadOperation.swift | 14 ++++++------- 2 files changed, 19 insertions(+), 15 deletions(-) diff --git a/Account/Sources/Account/CloudKit/CloudKitAccountDelegate.swift b/Account/Sources/Account/CloudKit/CloudKitAccountDelegate.swift index f7c938b7e..070ba3001 100644 --- a/Account/Sources/Account/CloudKit/CloudKitAccountDelegate.swift +++ b/Account/Sources/Account/CloudKit/CloudKitAccountDelegate.swift @@ -120,10 +120,12 @@ final class CloudKitAccountDelegate: AccountDelegate { func refreshArticleStatus(for account: Account, completion: @escaping ((Result) -> Void)) { let op = CloudKitReceiveStatusOperation(articlesZone: articlesZone) op.completionBlock = { mainThreadOperation in - if mainThreadOperation.isCanceled { - completion(.failure(CloudKitAccountDelegateError.unknown)) - } else { - completion(.success(())) + Task { @MainActor in + if mainThreadOperation.isCanceled { + completion(.failure(CloudKitAccountDelegateError.unknown)) + } else { + completion(.success(())) + } } } Task { @MainActor in @@ -775,10 +777,12 @@ private extension CloudKitAccountDelegate { showProgress: showProgress, database: database) op.completionBlock = { mainThreadOperaion in - if mainThreadOperaion.isCanceled { - completion(.failure(CloudKitAccountDelegateError.unknown)) - } else { - completion(.success(())) + Task { @MainActor in + if mainThreadOperaion.isCanceled { + completion(.failure(CloudKitAccountDelegateError.unknown)) + } else { + completion(.success(())) + } } } Task { @MainActor in diff --git a/Core/Sources/Core/MainThreadOperation.swift b/Core/Sources/Core/MainThreadOperation.swift index 9b38ba70b..a27af4952 100644 --- a/Core/Sources/Core/MainThreadOperation.swift +++ b/Core/Sources/Core/MainThreadOperation.swift @@ -18,12 +18,12 @@ import Foundation public protocol MainThreadOperation: AnyObject { // These three properties are set by MainThreadOperationQueue. Don’t set them. - var isCanceled: Bool { get set } // Check this at appropriate times in case the operation has been canceled. - var id: Int? { get set } - var operationDelegate: MainThreadOperationDelegate? { get set } // Make this weak. + @MainActor var isCanceled: Bool { get set } // Check this at appropriate times in case the operation has been canceled. + @MainActor var id: Int? { get set } + @MainActor var operationDelegate: MainThreadOperationDelegate? { get set } // Make this weak. /// Name may be useful for debugging. Unused otherwise. - var name: String? { get set } + @MainActor var name: String? { get set } typealias MainThreadOperationCompletionBlock = (MainThreadOperation) -> Void @@ -50,7 +50,7 @@ public protocol MainThreadOperation: AnyObject { /// When this is called, you don’t need to check isCanceled: /// it’s guaranteed to not be canceled. However, if you run code /// in another thread, you should check isCanceled in that code. - func run() + @MainActor func run() /// Cancel this operation. /// @@ -59,7 +59,7 @@ public protocol MainThreadOperation: AnyObject { /// /// This function has a default implementation. It’s super-rare /// to need to provide your own. - func cancel() + @MainActor func cancel() /// Make this operation dependent on an other operation. /// @@ -70,7 +70,7 @@ public protocol MainThreadOperation: AnyObject { /// /// This function has a default implementation. It’s super-rare /// to need to provide your own. - func addDependency(_ parentOperation: MainThreadOperation) + @MainActor func addDependency(_ parentOperation: MainThreadOperation) } public extension MainThreadOperation {