diff --git a/Mastodon/Scene/Settings/SettingsCoordinator.swift b/Mastodon/Scene/Settings/SettingsCoordinator.swift index 4f6caa783..e346bb7fa 100644 --- a/Mastodon/Scene/Settings/SettingsCoordinator.swift +++ b/Mastodon/Scene/Settings/SettingsCoordinator.swift @@ -261,7 +261,7 @@ extension SettingsCoordinator: NotificationSettingsViewControllerDelegate { extension SettingsCoordinator: PolicySelectionViewControllerDelegate { func newPolicySelected(_ viewController: PolicySelectionViewController, newPolicy: NotificationPolicy) { self.setting.activeSubscription?.policyRaw = newPolicy.subscriptionPolicy.rawValue - try? PersistenceManager.shared.managedObjectContext.save() + try? PersistenceManager.shared.mainActorManagedObjectContext.save() } } diff --git a/Mastodon/Scene/Thread/ThreadViewModel+Diffable.swift b/Mastodon/Scene/Thread/ThreadViewModel+Diffable.swift index 94b6e7230..a6a08ae90 100644 --- a/Mastodon/Scene/Thread/ThreadViewModel+Diffable.swift +++ b/Mastodon/Scene/Thread/ThreadViewModel+Diffable.swift @@ -78,7 +78,7 @@ extension ThreadViewModel { newSnapshot.appendSections([.main]) // top loader - let _hasReplyTo: Bool? = try? await PersistenceManager.shared.managedObjectContext.perform { + let _hasReplyTo: Bool? = try? await PersistenceManager.shared.mainActorManagedObjectContext.perform { guard case let .root(threadContext) = root else { return nil } return threadContext.status.entity.inReplyToID != nil } diff --git a/MastodonSDK/Sources/MastodonCore/AppContext.swift b/MastodonSDK/Sources/MastodonCore/AppContext.swift index fe8502ff7..e5ec85352 100644 --- a/MastodonSDK/Sources/MastodonCore/AppContext.swift +++ b/MastodonSDK/Sources/MastodonCore/AppContext.swift @@ -12,10 +12,11 @@ import CoreData import CoreDataStack import AlamofireImage +@MainActor public class PersistenceManager { public static let shared = { PersistenceManager() }() public let coreDataStack: CoreDataStack - public let managedObjectContext: NSManagedObjectContext + public let mainActorManagedObjectContext: NSManagedObjectContext public let backgroundManagedObjectContext: NSManagedObjectContext private var disposeBag = Set() @@ -26,15 +27,15 @@ public class PersistenceManager { let _backgroundManagedObjectContext = _coreDataStack.persistentContainer.newBackgroundContext() coreDataStack = _coreDataStack - managedObjectContext = _managedObjectContext + mainActorManagedObjectContext = _managedObjectContext backgroundManagedObjectContext = _backgroundManagedObjectContext backgroundManagedObjectContext.mergePolicy = NSMergePolicy.mergeByPropertyObjectTrump NotificationCenter.default.publisher(for: .NSManagedObjectContextDidSave, object: backgroundManagedObjectContext) .sink { [weak self] notification in guard let self = self else { return } - self.managedObjectContext.perform { - self.managedObjectContext.mergeChanges(fromContextDidSave: notification) + self.mainActorManagedObjectContext.perform { + self.mainActorManagedObjectContext.mergeChanges(fromContextDidSave: notification) } } .store(in: &disposeBag) @@ -60,9 +61,6 @@ public class AppContext: ObservableObject { .eraseToAnyPublisher() private init() { - - let authProvider = AuthenticationServiceProvider.shared - documentStore = DocumentStore() documentStoreSubscription = documentStore.objectWillChange .receive(on: DispatchQueue.main) diff --git a/MastodonSDK/Sources/MastodonCore/AuthenticationServiceProvider.swift b/MastodonSDK/Sources/MastodonCore/AuthenticationServiceProvider.swift index 070b4bda8..f0075eb3c 100644 --- a/MastodonSDK/Sources/MastodonCore/AuthenticationServiceProvider.swift +++ b/MastodonSDK/Sources/MastodonCore/AuthenticationServiceProvider.swift @@ -53,7 +53,7 @@ public class AuthenticationServiceProvider: ObservableObject { Task { if authenticationMigrationRequired { migrateLegacyAuthentications( - in: PersistenceManager.shared.managedObjectContext + in: PersistenceManager.shared.mainActorManagedObjectContext ) } } diff --git a/MastodonSDK/Sources/MastodonCore/FetchedResultsController/SettingFetchedResultController.swift b/MastodonSDK/Sources/MastodonCore/FetchedResultsController/SettingFetchedResultController.swift index 646c2651a..c80a9d8bb 100644 --- a/MastodonSDK/Sources/MastodonCore/FetchedResultsController/SettingFetchedResultController.swift +++ b/MastodonSDK/Sources/MastodonCore/FetchedResultsController/SettingFetchedResultController.swift @@ -29,7 +29,7 @@ public final class SettingFetchedResultController: NSObject { fetchRequest.fetchBatchSize = 20 let controller = NSFetchedResultsController( fetchRequest: fetchRequest, - managedObjectContext: PersistenceManager.shared.managedObjectContext, + managedObjectContext: PersistenceManager.shared.mainActorManagedObjectContext, sectionNameKeyPath: nil, cacheName: nil ) diff --git a/MastodonSDK/Sources/MastodonCore/Service/Notification/NotificationService.swift b/MastodonSDK/Sources/MastodonCore/Service/Notification/NotificationService.swift index fe88bbc5c..f940c3d9d 100644 --- a/MastodonSDK/Sources/MastodonCore/Service/Notification/NotificationService.swift +++ b/MastodonSDK/Sources/MastodonCore/Service/Notification/NotificationService.swift @@ -183,7 +183,7 @@ extension NotificationService { ) async throws { // Subscription maybe failed to cancel when sign-out // Try cancel again if receive that kind push notification - let managedObjectContext = PersistenceManager.shared.managedObjectContext + let managedObjectContext = PersistenceManager.shared.mainActorManagedObjectContext let userAccessToken = pushNotification.accessToken @@ -209,7 +209,7 @@ extension NotificationService { } private func domain(for pushNotification: MastodonPushNotification) async throws -> String? { - let managedObjectContext = PersistenceManager.shared.managedObjectContext + let managedObjectContext = PersistenceManager.shared.mainActorManagedObjectContext return try await managedObjectContext.perform { let subscriptionRequest = NotificationSubscription.sortedFetchRequest subscriptionRequest.predicate = NotificationSubscription.predicate(userToken: pushNotification.accessToken)