Fix migration issues with duplicate persistent store

This commit is contained in:
Marcus Kida 2023-06-05 17:21:32 +02:00 committed by Nathan Mattes
parent 55afa02b52
commit 73909005de
3 changed files with 19 additions and 11 deletions

View File

@ -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])
}

View File

@ -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

View File

@ -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