From d26cfd00c22a76321ff4b046e835196a2fc8ebb8 Mon Sep 17 00:00:00 2001 From: Brent Simmons Date: Sun, 29 Mar 2026 14:27:14 -0700 Subject: [PATCH] =?UTF-8?q?Fix=20concurrency=20issue.=20Make=20sure=20we?= =?UTF-8?q?=E2=80=99re=20on=20the=20main=20thread.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Sources/Account/AccountManager.swift | 48 ++++++++++--------- 1 file changed, 25 insertions(+), 23 deletions(-) diff --git a/Modules/Account/Sources/Account/AccountManager.swift b/Modules/Account/Sources/Account/AccountManager.swift index 7cd7e440e..bb51398bb 100644 --- a/Modules/Account/Sources/Account/AccountManager.swift +++ b/Modules/Account/Sources/Account/AccountManager.swift @@ -36,9 +36,11 @@ import ErrorLog public var syncArticleContentForUnreadArticles: Bool { get { - UserDefaults.standard.bool(forKey: Self.syncArticleContentForUnreadArticlesKey) + assert(Thread.isMainThread) + return UserDefaults.standard.bool(forKey: Self.syncArticleContentForUnreadArticlesKey) } set { + assert(Thread.isMainThread) UserDefaults.standard.set(newValue, forKey: Self.syncArticleContentForUnreadArticlesKey) NSUbiquitousKeyValueStore.default.set(newValue, forKey: Self.syncArticleContentForUnreadArticlesKey) } @@ -490,35 +492,33 @@ import ErrorLog private extension AccountManager { - @objc func handleUbiquitousKeyValueStoreDidChangeExternally(_ note: Notification) { - guard let changeReason = note.userInfo?[NSUbiquitousKeyValueStoreChangeReasonKey] as? Int else { - return - } + @objc nonisolated func handleUbiquitousKeyValueStoreDidChangeExternally(_ note: Notification) { + // Extract only Sendable primitives before hopping to the MainActor. + let changeReason = note.userInfo?[NSUbiquitousKeyValueStoreChangeReasonKey] as? Int + let changedKeys = note.userInfo?[NSUbiquitousKeyValueStoreChangedKeysKey] as? [String] - // Ignore account changes — the store may have been wiped, and - // bool(forKey:) would return false for a missing key, silently - // turning off the setting. - guard changeReason == NSUbiquitousKeyValueStoreServerChange || changeReason == NSUbiquitousKeyValueStoreInitialSyncChange else { - return - } + Task { @MainActor in + assert(Thread.isMainThread) + guard let changeReason, changeReason == NSUbiquitousKeyValueStoreServerChange || changeReason == NSUbiquitousKeyValueStoreInitialSyncChange else { + return + } - guard let changedKeys = note.userInfo?[NSUbiquitousKeyValueStoreChangedKeysKey] as? [String] else { - return - } - guard changedKeys.contains(Self.syncArticleContentForUnreadArticlesKey) else { - return - } + guard let changedKeys, changedKeys.contains(Self.syncArticleContentForUnreadArticlesKey) else { + return + } - // Only apply if the store actually has a value for the key. - guard NSUbiquitousKeyValueStore.default.object(forKey: Self.syncArticleContentForUnreadArticlesKey) != nil else { - return - } + // Only apply if the store actually has a value for the key. + guard NSUbiquitousKeyValueStore.default.object(forKey: Self.syncArticleContentForUnreadArticlesKey) != nil else { + return + } - let newValue = NSUbiquitousKeyValueStore.default.bool(forKey: Self.syncArticleContentForUnreadArticlesKey) - UserDefaults.standard.set(newValue, forKey: Self.syncArticleContentForUnreadArticlesKey) + let newValue = NSUbiquitousKeyValueStore.default.bool(forKey: Self.syncArticleContentForUnreadArticlesKey) + UserDefaults.standard.set(newValue, forKey: Self.syncArticleContentForUnreadArticlesKey) + } } func migrateSyncArticleContentForUnreadArticlesSetting(hasiCloudAccount: Bool) { + assert(Thread.isMainThread) // syncArticleContentForUnreadArticles should be set to false unless // the user already has an iCloud account. guard UserDefaults.standard.object(forKey: Self.syncArticleContentForUnreadArticlesKey) == nil else { @@ -536,6 +536,7 @@ private extension AccountManager { } func seedSyncArticleContentForUnreadArticlesInUbiquitousKeyValueStore() { + assert(Thread.isMainThread) guard NSUbiquitousKeyValueStore.default.object(forKey: Self.syncArticleContentForUnreadArticlesKey) == nil else { return } @@ -641,3 +642,4 @@ private struct AccountSpecifier { return NSString(string: folderPath).appendingPathComponent(accountDataFileName) } } +