From 563d1febbc2fbcc1014e8fa499d59591543a2481 Mon Sep 17 00:00:00 2001 From: Brent Simmons Date: Tue, 18 Nov 2025 16:39:51 -0800 Subject: [PATCH] Fix various concurrency issues. --- Mac/AppDelegate.swift | 1 + Shared/Settings/AddCloudKitAccount.swift | 9 ++++--- iOS/AppDelegate.swift | 12 +++------ .../FeedInspectorViewController.swift | 26 +++++++++---------- iOS/KeyboardManager.swift | 4 +-- .../MainFeedCollectionViewFolderCell.swift | 2 +- 6 files changed, 25 insertions(+), 29 deletions(-) diff --git a/Mac/AppDelegate.swift b/Mac/AppDelegate.swift index 36fbd2362..5a4a9a5aa 100644 --- a/Mac/AppDelegate.swift +++ b/Mac/AppDelegate.swift @@ -197,6 +197,7 @@ final class AppDelegate: NSObject, NSApplicationDelegate, NSUserInterfaceValidat self.toggleInspectorWindow(self) } + ArticleThemesManager.shared.start() NetworkMonitor.shared.start() ExtensionContainersFile.shared.start() ExtensionFeedAddRequestFile.shared.start() diff --git a/Shared/Settings/AddCloudKitAccount.swift b/Shared/Settings/AddCloudKitAccount.swift index ae2974e38..f161ed0c0 100644 --- a/Shared/Settings/AddCloudKitAccount.swift +++ b/Shared/Settings/AddCloudKitAccount.swift @@ -14,7 +14,7 @@ import AppKit import UIKit #endif -enum AddCloudKitAccountError: LocalizedError, RecoverableError { +enum AddCloudKitAccountError: LocalizedError, RecoverableError, Sendable { case iCloudDriveMissing @@ -43,7 +43,10 @@ enum AddCloudKitAccountError: LocalizedError, RecoverableError { return false } - AddCloudKitAccountUtilities.openiCloudSettings() + Task { @MainActor in + AddCloudKitAccountUtilities.openiCloudSettings() + } + return true } } @@ -54,7 +57,7 @@ struct AddCloudKitAccountUtilities { FileManager.default.ubiquityIdentityToken != nil } - static func openiCloudSettings() { + @MainActor static func openiCloudSettings() { #if os(macOS) if let url = URL(string: "x-apple.systempreferences:com.apple.preferences.AppleIDPrefPane") { NSWorkspace.shared.open(url) diff --git a/iOS/AppDelegate.swift b/iOS/AppDelegate.swift index 1affa471f..7e7e5f799 100644 --- a/iOS/AppDelegate.swift +++ b/iOS/AppDelegate.swift @@ -16,9 +16,9 @@ import Account import Articles import Secrets -var appDelegate: AppDelegate! +@MainActor var appDelegate: AppDelegate! -@UIApplicationMain +@main final class AppDelegate: UIResponder, UIApplicationDelegate, UNUserNotificationCenterDelegate, UnreadCountProvider { private let backgroundTaskDispatchQueue = DispatchQueue.init(label: "BGTaskScheduler") @@ -52,15 +52,8 @@ final class AppDelegate: UIResponder, UIApplicationDelegate, UNUserNotificationC super.init() appDelegate = self - let documentFolder = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first! - let documentAccountsFolder = documentFolder.appendingPathComponent("Accounts").absoluteString - AccountManager.shared.start() - let documentThemesFolder = documentFolder.appendingPathComponent("Themes").absoluteString - let documentThemesFolderPath = String(documentThemesFolder.suffix(from: documentAccountsFolder.index(documentThemesFolder.startIndex, offsetBy: 7))) - ArticleThemesManager.shared = ArticleThemesManager(folderPath: documentThemesFolderPath) - NotificationCenter.default.addObserver(self, selector: #selector(unreadCountDidChange(_:)), name: .UnreadCountDidChange, object: nil) NotificationCenter.default.addObserver(self, selector: #selector(accountRefreshDidFinish(_:)), name: .AccountRefreshDidFinish, object: nil) } @@ -100,6 +93,7 @@ final class AppDelegate: UIResponder, UIApplicationDelegate, UNUserNotificationC UNUserNotificationCenter.current().delegate = self UserNotificationManager.shared.start() + ArticleThemesManager.shared.start() NetworkMonitor.shared.start() ExtensionContainersFile.shared.start() ExtensionFeedAddRequestFile.shared.start() diff --git a/iOS/Inspector/FeedInspectorViewController.swift b/iOS/Inspector/FeedInspectorViewController.swift index c734d8bea..a5e532e99 100644 --- a/iOS/Inspector/FeedInspectorViewController.swift +++ b/iOS/Inspector/FeedInspectorViewController.swift @@ -33,7 +33,7 @@ final class FeedInspectorViewController: UITableViewController { return feed.homePageURL == nil } - private var userNotificationSettings: UNNotificationSettings? + private var authorizationStatus: UNAuthorizationStatus? override func viewDidLoad() { tableView.register(InspectorIconHeaderView.self, forHeaderFooterViewReuseIdentifier: "SectionHeader") @@ -72,25 +72,23 @@ final class FeedInspectorViewController: UITableViewController { } @IBAction func notifyAboutNewArticlesChanged(_ sender: Any) { - guard let settings = userNotificationSettings else { + guard let authorizationStatus else { notifyAboutNewArticlesSwitch.isOn = !notifyAboutNewArticlesSwitch.isOn return } - if settings.authorizationStatus == .denied { + if authorizationStatus == .denied { notifyAboutNewArticlesSwitch.isOn = !notifyAboutNewArticlesSwitch.isOn present(notificationUpdateErrorAlert(), animated: true, completion: nil) - } else if settings.authorizationStatus == .authorized { + } else if authorizationStatus == .authorized { feed.isNotifyAboutNewArticles = notifyAboutNewArticlesSwitch.isOn } else { UNUserNotificationCenter.current().requestAuthorization(options:[.badge, .sound, .alert]) { (granted, error) in - self.updateNotificationSettings() - if granted { - DispatchQueue.main.async { + Task { @MainActor in + self.updateNotificationSettings() + if granted { self.feed.isNotifyAboutNewArticles = self.notifyAboutNewArticlesSwitch.isOn UIApplication.shared.registerForRemoteNotifications() - } - } else { - DispatchQueue.main.async { + } else { self.notifyAboutNewArticlesSwitch.isOn = !self.notifyAboutNewArticlesSwitch.isOn } } @@ -199,12 +197,12 @@ extension FeedInspectorViewController: UITextFieldDelegate { extension FeedInspectorViewController { - @objc - func updateNotificationSettings() { + @objc func updateNotificationSettings() { UNUserNotificationCenter.current().getNotificationSettings { (settings) in + let updatedAuthorizationStatus = settings.authorizationStatus DispatchQueue.main.async { - self.userNotificationSettings = settings - if settings.authorizationStatus == .authorized { + self.authorizationStatus = updatedAuthorizationStatus + if self.authorizationStatus == .authorized { UIApplication.shared.registerForRemoteNotifications() } } diff --git a/iOS/KeyboardManager.swift b/iOS/KeyboardManager.swift index c32b7ba32..d2c71f65f 100644 --- a/iOS/KeyboardManager.swift +++ b/iOS/KeyboardManager.swift @@ -8,14 +8,14 @@ import UIKit -enum KeyboardType: String { +enum KeyboardType: String, Sendable { case global = "GlobalKeyboardShortcuts" case sidebar = "SidebarKeyboardShortcuts" case timeline = "TimelineKeyboardShortcuts" case detail = "DetailKeyboardShortcuts" } -final class KeyboardManager { +@MainActor final class KeyboardManager { private(set) var _keyCommands: [UIKeyCommand] var keyCommands: [UIKeyCommand] { diff --git a/iOS/MainFeed/Collection View Cells/MainFeedCollectionViewFolderCell.swift b/iOS/MainFeed/Collection View Cells/MainFeedCollectionViewFolderCell.swift index b85bf02a4..bfd6ff86a 100644 --- a/iOS/MainFeed/Collection View Cells/MainFeedCollectionViewFolderCell.swift +++ b/iOS/MainFeed/Collection View Cells/MainFeedCollectionViewFolderCell.swift @@ -8,7 +8,7 @@ import UIKit -protocol MainFeedCollectionViewFolderCellDelegate: AnyObject { +@MainActor protocol MainFeedCollectionViewFolderCellDelegate: AnyObject { func mainFeedCollectionFolderViewCellDisclosureDidToggle(_ sender: MainFeedCollectionViewFolderCell, expanding: Bool) }