From 1be7e680d0934f5dc45b656e0f498e863e2767ec Mon Sep 17 00:00:00 2001 From: Brent Simmons Date: Sun, 9 Jul 2023 22:54:55 -0700 Subject: [PATCH] Use MainActor Task instead of GCD. --- Mac/AppDelegate.swift | 22 +++++++++---------- .../AddFeed/AddFeedController.swift | 8 +++---- Mac/MainWindow/MainWindowController.swift | 5 ++--- .../OPML/ExportOPMLWindowController.swift | 2 +- .../Timeline/TimelineViewController.swift | 2 +- 5 files changed, 19 insertions(+), 20 deletions(-) diff --git a/Mac/AppDelegate.swift b/Mac/AppDelegate.swift index cf3b42868..3415c38a5 100644 --- a/Mac/AppDelegate.swift +++ b/Mac/AppDelegate.swift @@ -223,7 +223,7 @@ var appDelegate: AppDelegate! NotificationCenter.default.addObserver(self, selector: #selector(feedSettingDidChange(_:)), name: .FeedSettingDidChange, object: nil) NotificationCenter.default.addObserver(self, selector: #selector(userDefaultsDidChange(_:)), name: UserDefaults.didChangeNotification, object: nil) - DispatchQueue.main.async { + Task { @MainActor in self.unreadCount = AccountManager.shared.unreadCount } @@ -241,7 +241,7 @@ var appDelegate: AppDelegate! UNUserNotificationCenter.current().getNotificationSettings { (settings) in if settings.authorizationStatus == .authorized { - DispatchQueue.main.async { + Task { @MainActor in NSApplication.shared.registerForRemoteNotifications() } } @@ -258,9 +258,9 @@ var appDelegate: AppDelegate! refreshTimer!.update() syncTimer!.update() } else { - DispatchQueue.main.async { - self.refreshTimer!.timedRefresh(nil) - self.syncTimer!.timedRefresh(nil) + Task { @MainActor in + refreshTimer!.timedRefresh(nil) + syncTimer!.timedRefresh(nil) } } #endif @@ -279,7 +279,7 @@ var appDelegate: AppDelegate! } #if !MAC_APP_STORE - DispatchQueue.main.async { + Task { @MainActor in CrashReporter.check(crashReporter: self.crashReporter) } #endif @@ -371,13 +371,13 @@ var appDelegate: AppDelegate! informativeText = error.localizedDescription } - DispatchQueue.main.async { + Task { @MainActor in let alert = NSAlert() alert.alertStyle = .warning alert.messageText = NSLocalizedString("alert.title.theme-error", comment: "Theme error") alert.informativeText = informativeText alert.addButton(withTitle: NSLocalizedString("button.title.ok", comment: "OK")) - + alert.buttons[0].keyEquivalent = "\r" _ = alert.runModal() @@ -429,7 +429,7 @@ var appDelegate: AppDelegate! let url = userInfo["url"] as? URL else { return } - DispatchQueue.main.async { + Task { @MainActor in self.importTheme(filename: url.path) } } @@ -960,7 +960,7 @@ extension AppDelegate { assert(shouldShowTwitterDeprecationAlert()) AppDefaults.shared.twitterDeprecationAlertShown = true - DispatchQueue.main.async { + Task { @MainActor in let alert = NSAlert() alert.alertStyle = .warning alert.messageText = NSLocalizedString("Twitter Integration Removed", comment: "Twitter Integration Removed") @@ -987,7 +987,7 @@ extension AppDelegate { assert(shouldShowRedditDeprecationAlert()) AppDefaults.shared.redditDeprecationAlertShown = true - DispatchQueue.main.async { + Task { @MainActor in let alert = NSAlert() alert.alertStyle = .warning alert.messageText = NSLocalizedString("Reddit API Integration Removed", comment: "Reddit API Integration Removed") diff --git a/Mac/MainWindow/AddFeed/AddFeedController.swift b/Mac/MainWindow/AddFeed/AddFeedController.swift index 5e2212f10..09cc59aff 100644 --- a/Mac/MainWindow/AddFeed/AddFeedController.swift +++ b/Mac/MainWindow/AddFeed/AddFeedController.swift @@ -66,11 +66,11 @@ import RSParser } account.createFeed(url: url.absoluteString, name: title, container: container, validateFeed: true) { result in - - DispatchQueue.main.async { + + Task { @MainActor in self.endShowingProgress() } - + switch result { case .success(let feed): NotificationCenter.default.post(name: .UserDidAddFeed, object: self, userInfo: [UserInfoKey.feed: feed]) @@ -81,7 +81,7 @@ import RSParser case AccountError.createErrorNotFound: self.showNoFeedsErrorMessage() default: - DispatchQueue.main.async { + Task { @MainActor in NSApplication.shared.presentError(error) } } diff --git a/Mac/MainWindow/MainWindowController.swift b/Mac/MainWindow/MainWindowController.swift index e70335d59..23478446a 100644 --- a/Mac/MainWindow/MainWindowController.swift +++ b/Mac/MainWindow/MainWindowController.swift @@ -106,11 +106,10 @@ enum TimelineSourceMode { NotificationCenter.default.addObserver(self, selector: #selector(articleThemeNamesDidChangeNotification(_:)), name: .ArticleThemeNamesDidChangeNotification, object: nil) NotificationCenter.default.addObserver(self, selector: #selector(currentArticleThemeDidChangeNotification(_:)), name: .CurrentArticleThemeDidChangeNotification, object: nil) - - DispatchQueue.main.async { + + Task { @MainActor in self.updateWindowTitle() } - } // MARK: - API diff --git a/Mac/MainWindow/OPML/ExportOPMLWindowController.swift b/Mac/MainWindow/OPML/ExportOPMLWindowController.swift index 624a63847..c846eabb1 100644 --- a/Mac/MainWindow/OPML/ExportOPMLWindowController.swift +++ b/Mac/MainWindow/OPML/ExportOPMLWindowController.swift @@ -88,7 +88,7 @@ import Account panel.beginSheetModal(for: hostWindow!) { result in if result == NSApplication.ModalResponse.OK, let url = panel.url { - DispatchQueue.main.async { + Task { @MainActor in let filename = url.lastPathComponent let opmlString = OPMLExporter.OPMLString(with: account, title: filename) do { diff --git a/Mac/MainWindow/Timeline/TimelineViewController.swift b/Mac/MainWindow/Timeline/TimelineViewController.swift index efe49661c..3f59fb39a 100644 --- a/Mac/MainWindow/Timeline/TimelineViewController.swift +++ b/Mac/MainWindow/Timeline/TimelineViewController.swift @@ -320,7 +320,7 @@ final class TimelineViewController: NSViewController, UndoableCommandRunner, Unr if let selectedIndex = articles.firstIndex(where: { $0.articleID == articleID }) { tableView.selectRow(selectedIndex) - DispatchQueue.main.async { + Task { @MainActor in self.tableView.scrollTo(row: selectedIndex) } focus()