diff --git a/MastodonSDK/Sources/CoreDataStack/CoreDataStack.swift b/MastodonSDK/Sources/CoreDataStack/CoreDataStack.swift index ce512f303..f7d8f1de7 100644 --- a/MastodonSDK/Sources/CoreDataStack/CoreDataStack.swift +++ b/MastodonSDK/Sources/CoreDataStack/CoreDataStack.swift @@ -29,7 +29,6 @@ public final class CoreDataStack { storeDescription = NSPersistentStoreDescription(url: URL(string: "file:///dev/null")!) /// in-memory store with all features in favor of NSInMemoryStoreType } else { storeDescription = NSPersistentStoreDescription(url: storeURL) - storeDescription.isReadOnly = true } self.init(persistentStoreDescriptions: [storeDescription]) } diff --git a/MastodonSDK/Sources/MastodonCore/AppContext.swift b/MastodonSDK/Sources/MastodonCore/AppContext.swift index 12eea2519..02966e2f8 100644 --- a/MastodonSDK/Sources/MastodonCore/AppContext.swift +++ b/MastodonSDK/Sources/MastodonCore/AppContext.swift @@ -47,15 +47,22 @@ public class AppContext: ObservableObject { .eraseToAnyPublisher() public init() { + + let authProvider = AuthenticationServiceProvider.shared + let _coreDataStack: CoreDataStack + if authProvider.authenticationMigrationRequired { + _coreDataStack = CoreDataStack(isInMemory: false) + authProvider.migrateLegacyAuthentications( + in: _coreDataStack.persistentContainer.viewContext + ) + } else { + _coreDataStack = CoreDataStack(isInMemory: true) + } - /// Migrate existing Authentications to new Keychain-Based format -// var _legacyCoreDataStack: CoreDataStack? = CoreDataStack(isInMemory: false) -// AuthenticationServiceProvider.shared.migrateLegacyAuthenticationsIfRequired(in: _legacyCoreDataStack!.persistentContainer.viewContext) -// _legacyCoreDataStack!.tearDown() -// _legacyCoreDataStack = nil - - let _coreDataStack = CoreDataStack(isInMemory: true) let _managedObjectContext = _coreDataStack.persistentContainer.viewContext + _coreDataStack.persistentContainer.persistentStoreDescriptions.forEach { + $0.url = URL(fileURLWithPath: "/dev/null") + } let _backgroundManagedObjectContext = _coreDataStack.persistentContainer.newBackgroundContext() coreDataStack = _coreDataStack managedObjectContext = _managedObjectContext diff --git a/MastodonSDK/Sources/MastodonCore/AuthenticationServiceProvider.swift b/MastodonSDK/Sources/MastodonCore/AuthenticationServiceProvider.swift index 81d0c2da7..01b7bc6f3 100644 --- a/MastodonSDK/Sources/MastodonCore/AuthenticationServiceProvider.swift +++ b/MastodonSDK/Sources/MastodonCore/AuthenticationServiceProvider.swift @@ -68,9 +68,7 @@ public extension AuthenticationServiceProvider { } } - func migrateLegacyAuthenticationsIfRequired(in context: NSManagedObjectContext) { -// guard !userDefaults.didMigrateAuthentications else { return } - + func migrateLegacyAuthentications(in context: NSManagedObjectContext) { defer { userDefaults.didMigrateAuthentications = true } do { @@ -112,6 +110,10 @@ public extension AuthenticationServiceProvider { logger.log(level: .error, "Could not migrate legacy authentications") } } + + var authenticationMigrationRequired: Bool { + !userDefaults.didMigrateAuthentications + } } // MARK: - Private