Fix migration issues with duplicate persistent store
This commit is contained in:
parent
55afa02b52
commit
73909005de
|
@ -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])
|
||||
}
|
||||
|
|
|
@ -48,14 +48,21 @@ public class AppContext: ObservableObject {
|
|||
|
||||
public init() {
|
||||
|
||||
/// 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 authProvider = AuthenticationServiceProvider.shared
|
||||
let _coreDataStack: CoreDataStack
|
||||
if authProvider.authenticationMigrationRequired {
|
||||
_coreDataStack = CoreDataStack(isInMemory: false)
|
||||
authProvider.migrateLegacyAuthentications(
|
||||
in: _coreDataStack.persistentContainer.viewContext
|
||||
)
|
||||
} else {
|
||||
_coreDataStack = CoreDataStack(isInMemory: true)
|
||||
}
|
||||
|
||||
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
|
||||
|
|
|
@ -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
|
||||
|
|
Loading…
Reference in New Issue