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 storeDescription = NSPersistentStoreDescription(url: URL(string: "file:///dev/null")!) /// in-memory store with all features in favor of NSInMemoryStoreType
} else { } else {
storeDescription = NSPersistentStoreDescription(url: storeURL) storeDescription = NSPersistentStoreDescription(url: storeURL)
storeDescription.isReadOnly = true
} }
self.init(persistentStoreDescriptions: [storeDescription]) self.init(persistentStoreDescriptions: [storeDescription])
} }

View File

@ -48,14 +48,21 @@ public class AppContext: ObservableObject {
public init() { public init() {
/// Migrate existing Authentications to new Keychain-Based format let authProvider = AuthenticationServiceProvider.shared
// var _legacyCoreDataStack: CoreDataStack? = CoreDataStack(isInMemory: false) let _coreDataStack: CoreDataStack
// AuthenticationServiceProvider.shared.migrateLegacyAuthenticationsIfRequired(in: _legacyCoreDataStack!.persistentContainer.viewContext) if authProvider.authenticationMigrationRequired {
// _legacyCoreDataStack!.tearDown() _coreDataStack = CoreDataStack(isInMemory: false)
// _legacyCoreDataStack = nil authProvider.migrateLegacyAuthentications(
in: _coreDataStack.persistentContainer.viewContext
)
} else {
_coreDataStack = CoreDataStack(isInMemory: true)
}
let _coreDataStack = CoreDataStack(isInMemory: true)
let _managedObjectContext = _coreDataStack.persistentContainer.viewContext let _managedObjectContext = _coreDataStack.persistentContainer.viewContext
_coreDataStack.persistentContainer.persistentStoreDescriptions.forEach {
$0.url = URL(fileURLWithPath: "/dev/null")
}
let _backgroundManagedObjectContext = _coreDataStack.persistentContainer.newBackgroundContext() let _backgroundManagedObjectContext = _coreDataStack.persistentContainer.newBackgroundContext()
coreDataStack = _coreDataStack coreDataStack = _coreDataStack
managedObjectContext = _managedObjectContext managedObjectContext = _managedObjectContext

View File

@ -68,9 +68,7 @@ public extension AuthenticationServiceProvider {
} }
} }
func migrateLegacyAuthenticationsIfRequired(in context: NSManagedObjectContext) { func migrateLegacyAuthentications(in context: NSManagedObjectContext) {
// guard !userDefaults.didMigrateAuthentications else { return }
defer { userDefaults.didMigrateAuthentications = true } defer { userDefaults.didMigrateAuthentications = true }
do { do {
@ -112,6 +110,10 @@ public extension AuthenticationServiceProvider {
logger.log(level: .error, "Could not migrate legacy authentications") logger.log(level: .error, "Could not migrate legacy authentications")
} }
} }
var authenticationMigrationRequired: Bool {
!userDefaults.didMigrateAuthentications
}
} }
// MARK: - Private // MARK: - Private