diff --git a/Mac/MainWindow/Detail/DetailViewController.swift b/Mac/MainWindow/Detail/DetailViewController.swift index 36ce8c8a2..5205b63f8 100644 --- a/Mac/MainWindow/Detail/DetailViewController.swift +++ b/Mac/MainWindow/Detail/DetailViewController.swift @@ -87,12 +87,12 @@ final class DetailViewController: NSViewController, WKUIDelegate { currentWebViewController.stopMediaPlayback() } - func canScrollDown(_ callback: @escaping (Bool) -> Void) { - currentWebViewController.canScrollDown(callback) + func canScrollDown() async -> Bool { + await currentWebViewController.canScrollDown() } - func canScrollUp(_ callback: @escaping (Bool) -> Void) { - currentWebViewController.canScrollUp(callback) + func canScrollUp() async -> Bool { + await currentWebViewController.canScrollUp() } override func scrollPageDown(_ sender: Any?) { diff --git a/Mac/MainWindow/Detail/DetailWebViewController.swift b/Mac/MainWindow/Detail/DetailWebViewController.swift index bc1ddce00..db4f15790 100644 --- a/Mac/MainWindow/Detail/DetailWebViewController.swift +++ b/Mac/MainWindow/Detail/DetailWebViewController.swift @@ -150,16 +150,14 @@ final class DetailWebViewController: NSViewController { // MARK: Scrolling - func canScrollDown(_ completion: @escaping (Bool) -> Void) { - fetchScrollInfo { (scrollInfo) in - completion(scrollInfo?.canScrollDown ?? false) - } + func canScrollDown() async -> Bool { + let scrollInfo = await fetchScrollInfo() + return scrollInfo?.canScrollDown ?? false } - func canScrollUp(_ completion: @escaping (Bool) -> Void) { - fetchScrollInfo { (scrollInfo) in - completion(scrollInfo?.canScrollUp ?? false) - } + func canScrollUp() async -> Bool { + let scrollInfo = await fetchScrollInfo() + return scrollInfo?.canScrollUp ?? false } override func scrollPageDown(_ sender: Any?) { @@ -306,7 +304,15 @@ private extension DetailWebViewController { webView.loadHTMLString(html, baseURL: URL(string: rendering.baseURL)) } - func fetchScrollInfo(_ completion: @escaping (ScrollInfo?) -> Void) { + func fetchScrollInfo() async -> ScrollInfo? { + await withCheckedContinuation { continuation in + self.fetchScrollInfo { scrollInfo in + continuation.resume(returning: scrollInfo) + } + } + } + + private func fetchScrollInfo(_ completion: @escaping (ScrollInfo?) -> Void) { let javascriptString = "var x = {contentHeight: document.body.scrollHeight, offsetY: document.body.scrollTop}; x" webView.evaluateJavaScript(javascriptString) { (info, error) in diff --git a/Mac/MainWindow/MainWindowController.swift b/Mac/MainWindow/MainWindowController.swift index be48eb9df..bdc283050 100644 --- a/Mac/MainWindow/MainWindowController.swift +++ b/Mac/MainWindow/MainWindowController.swift @@ -279,21 +279,27 @@ final class MainWindowController : NSWindowController, NSUserInterfaceValidation // MARK: - Actions @IBAction func scrollOrGoToNextUnread(_ sender: Any?) { - guard let detailViewController = detailViewController else { + guard let detailViewController else { return } - detailViewController.canScrollDown { (canScroll) in + Task { @MainActor in + let canScroll = await detailViewController.canScrollDown() NSCursor.setHiddenUntilMouseMoves(true) - canScroll ? detailViewController.scrollPageDown(sender) : self.nextUnread(sender) + if canScroll { + detailViewController.scrollPageDown(sender) + } else { + nextUnread(sender) + } } } @IBAction func scrollUp(_ sender: Any?) { - guard let detailViewController = detailViewController else { + guard let detailViewController else { return } - detailViewController.canScrollUp { (canScroll) in - if (canScroll) { + Task { @MainActor in + let canScroll = await detailViewController.canScrollUp() + if canScroll { NSCursor.setHiddenUntilMouseMoves(true) detailViewController.scrollPageUp(sender) } diff --git a/Modules/Account/Sources/Account/Account.swift b/Modules/Account/Sources/Account/Account.swift index 0e77e06ae..d18a6b8ca 100644 --- a/Modules/Account/Sources/Account/Account.swift +++ b/Modules/Account/Sources/Account/Account.swift @@ -310,7 +310,9 @@ public final class Account: DisplayNameProvider, UnreadCountProvider, Container, self.fetchAllUnreadCounts() } - self.delegate.accountDidInitialize(self) + MainActor.assumeIsolated { + self.delegate.accountDidInitialize(self) + } } // MARK: - API @@ -394,7 +396,7 @@ public final class Account: DisplayNameProvider, UnreadCountProvider, Container, await delegate.receiveRemoteNotification(for: self, userInfo: userInfo) } - public func refreshAll() async throws { + @MainActor public func refreshAll() async throws { try await delegate.refreshAll(for: self) } @@ -425,7 +427,7 @@ public final class Account: DisplayNameProvider, UnreadCountProvider, Container, } } - public func suspendNetwork() { + @MainActor public func suspendNetwork() { delegate.suspendNetwork() } @@ -438,7 +440,7 @@ public final class Account: DisplayNameProvider, UnreadCountProvider, Container, /// Re-open the SQLite database and allow database calls. /// Call this *before* calling resume. - public func resumeDatabaseAndDelegate() { + @MainActor public func resumeDatabaseAndDelegate() { #if os(iOS) database.resume() #endif @@ -456,7 +458,7 @@ public final class Account: DisplayNameProvider, UnreadCountProvider, Container, opmlFile.save() } - public func prepareForDeletion() { + @MainActor public func prepareForDeletion() { delegate.accountWillBeDeleted(self) } diff --git a/Modules/Account/Sources/Account/AccountDelegate.swift b/Modules/Account/Sources/Account/AccountDelegate.swift index 9727731a6..bb07d212f 100644 --- a/Modules/Account/Sources/Account/AccountDelegate.swift +++ b/Modules/Account/Sources/Account/AccountDelegate.swift @@ -48,18 +48,18 @@ protocol AccountDelegate { @MainActor func markArticles(for account: Account, articles: Set
, statusKey: ArticleStatus.Key, flag: Bool) async throws // Called at the end of account’s init method. - func accountDidInitialize(_ account: Account) + @MainActor func accountDidInitialize(_ account: Account) - func accountWillBeDeleted(_ account: Account) + @MainActor func accountWillBeDeleted(_ account: Account) - static func validateCredentials(transport: Transport, credentials: Credentials, endpoint: URL?) async throws -> Credentials? + @MainActor static func validateCredentials(transport: Transport, credentials: Credentials, endpoint: URL?) async throws -> Credentials? /// Suspend all network activity - func suspendNetwork() + @MainActor func suspendNetwork() /// Suspend the SQLite databases - func suspendDatabase() + @MainActor func suspendDatabase() /// Make sure no SQLite databases are open and we are ready to issue network requests. - func resume() + @MainActor func resume() } diff --git a/Modules/Account/Sources/Account/AccountManager.swift b/Modules/Account/Sources/Account/AccountManager.swift index 08e105b2c..6f77d60dd 100644 --- a/Modules/Account/Sources/Account/AccountManager.swift +++ b/Modules/Account/Sources/Account/AccountManager.swift @@ -167,7 +167,7 @@ public final class AccountManager: UnreadCountProvider { return account } - public func deleteAccount(_ account: Account) { + @MainActor public func deleteAccount(_ account: Account) { guard !account.refreshInProgress else { return } @@ -236,7 +236,7 @@ public final class AccountManager: UnreadCountProvider { return nil } - public func suspendNetworkAll() { + @MainActor public func suspendNetworkAll() { isSuspended = true for account in accounts { account.suspendNetwork() @@ -249,7 +249,7 @@ public final class AccountManager: UnreadCountProvider { } } - public func resumeAll() { + @MainActor public func resumeAll() { isSuspended = false for account in accounts { account.resumeDatabaseAndDelegate() @@ -278,9 +278,9 @@ public final class AccountManager: UnreadCountProvider { combinedRefreshProgress.start() Task { @MainActor in - await withTaskGroup(of: Void.self) { group in + await withTaskGroup(of: Void.self, isolation: MainActor.shared) { group in for account in activeAccounts { - group.addTask { + group.addTask { @MainActor in do { try await account.refreshAll() } catch { @@ -313,9 +313,6 @@ public final class AccountManager: UnreadCountProvider { try? await account.syncArticleStatus() group.leave() } - } - - group.notify(queue: DispatchQueue.global(qos: .background)) { completion?() } } diff --git a/Modules/Account/Sources/Account/CloudKit/CloudKitAccountDelegate.swift b/Modules/Account/Sources/Account/CloudKit/CloudKitAccountDelegate.swift index ac631f791..64411083e 100644 --- a/Modules/Account/Sources/Account/CloudKit/CloudKitAccountDelegate.swift +++ b/Modules/Account/Sources/Account/CloudKit/CloudKitAccountDelegate.swift @@ -381,15 +381,15 @@ final class CloudKitAccountDelegate: AccountDelegate { // MARK: - Suspend and Resume (for iOS) - func suspendNetwork() { + @MainActor func suspendNetwork() { refresher.suspend() } - func suspendDatabase() { + @MainActor func suspendDatabase() { syncDatabase.suspend() } - func resume() { + @MainActor func resume() { refresher.resume() syncDatabase.resume() } diff --git a/Modules/Account/Sources/Account/CloudKit/CloudKitArticlesZone.swift b/Modules/Account/Sources/Account/CloudKit/CloudKitArticlesZone.swift index 8be2f76f3..b129629e4 100644 --- a/Modules/Account/Sources/Account/CloudKit/CloudKitArticlesZone.swift +++ b/Modules/Account/Sources/Account/CloudKit/CloudKitArticlesZone.swift @@ -63,7 +63,7 @@ final class CloudKitArticlesZone: CloudKitZone { migrateChangeToken() } - func refreshArticles() async throws { + @MainActor func refreshArticles() async throws { do { try await fetchChangesInZone() } catch { diff --git a/Modules/Account/Sources/Account/CloudKit/CloudKitReceiveStatusOperation.swift b/Modules/Account/Sources/Account/CloudKit/CloudKitReceiveStatusOperation.swift index d487f81fb..1c9ed24a4 100644 --- a/Modules/Account/Sources/Account/CloudKit/CloudKitReceiveStatusOperation.swift +++ b/Modules/Account/Sources/Account/CloudKit/CloudKitReceiveStatusOperation.swift @@ -26,13 +26,13 @@ final class CloudKitReceiveStatusOperation: MainThreadOperation { self.articlesZone = articlesZone } - func run() { + @MainActor func run() { guard let articlesZone else { self.operationDelegate?.operationDidComplete(self) return } - Task { + Task { @MainActor in Self.logger.debug("iCloud: Refreshing article statuses") do { try await articlesZone.refreshArticles() diff --git a/Modules/Account/Sources/Account/LocalAccount/LocalAccountDelegate.swift b/Modules/Account/Sources/Account/LocalAccount/LocalAccountDelegate.swift index e55f6d64d..8d9adef57 100644 --- a/Modules/Account/Sources/Account/LocalAccount/LocalAccountDelegate.swift +++ b/Modules/Account/Sources/Account/LocalAccount/LocalAccountDelegate.swift @@ -38,7 +38,7 @@ final class LocalAccountDelegate: AccountDelegate { func receiveRemoteNotification(for account: Account, userInfo: [AnyHashable : Any]) async { } - func refreshAll(for account: Account) async throws { + @MainActor func refreshAll(for account: Account) async throws { guard refreshProgress.isComplete, !Platform.isRunningUnitTests else { return } @@ -137,15 +137,15 @@ final class LocalAccountDelegate: AccountDelegate { // MARK: Suspend and Resume (for iOS) - func suspendNetwork() { + @MainActor func suspendNetwork() { refresher.suspend() } - func suspendDatabase() { + @MainActor func suspendDatabase() { // Nothing to do } - func resume() { + @MainActor func resume() { refresher.resume() } } diff --git a/Modules/Account/Sources/Account/LocalAccount/LocalAccountRefresher.swift b/Modules/Account/Sources/Account/LocalAccount/LocalAccountRefresher.swift index d68111875..927608e12 100644 --- a/Modules/Account/Sources/Account/LocalAccount/LocalAccountRefresher.swift +++ b/Modules/Account/Sources/Account/LocalAccount/LocalAccountRefresher.swift @@ -36,7 +36,7 @@ final class LocalAccountRefresher { private static let logger = Logger(subsystem: Bundle.main.bundleIdentifier!, category: "LocalAccountRefresher") - public func refreshFeeds(_ feeds: Set) async { + @MainActor public func refreshFeeds(_ feeds: Set) async { await withCheckedContinuation { continuation in refreshFeeds(feeds) { continuation.resume() @@ -44,7 +44,7 @@ final class LocalAccountRefresher { } } - private func refreshFeeds(_ feeds: Set, completion: (() -> Void)? = nil) { + @MainActor private func refreshFeeds(_ feeds: Set, completion: (() -> Void)? = nil) { let specialCaseCutoffDate = Date().bySubtracting(hours: 25) let filteredFeeds = feeds.filter { !Self.feedShouldBeSkipped($0, specialCaseCutoffDate) } @@ -66,12 +66,12 @@ final class LocalAccountRefresher { downloadSession.download(Set(urls)) } - public func suspend() { + @MainActor public func suspend() { downloadSession.cancelAll() isSuspended = true } - public func resume() { + @MainActor public func resume() { isSuspended = false } } diff --git a/Modules/RSCore/Sources/RSCore/Shared/MainThreadOperation.swift b/Modules/RSCore/Sources/RSCore/Shared/MainThreadOperation.swift index 92a4f97c3..bb814c787 100644 --- a/Modules/RSCore/Sources/RSCore/Shared/MainThreadOperation.swift +++ b/Modules/RSCore/Sources/RSCore/Shared/MainThreadOperation.swift @@ -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. ///