From 677963be59fcf262484fcb368e97f2d40363705d Mon Sep 17 00:00:00 2001 From: Garth Vander Houwen Date: Wed, 29 Dec 2021 16:13:17 -0800 Subject: [PATCH] clearDatabase on error --- .../Persistence/Persistence.swift | 32 ++++++++++--------- 1 file changed, 17 insertions(+), 15 deletions(-) diff --git a/MeshtasticClient/Persistence/Persistence.swift b/MeshtasticClient/Persistence/Persistence.swift index 9168f07d..8c0c00f7 100644 --- a/MeshtasticClient/Persistence/Persistence.swift +++ b/MeshtasticClient/Persistence/Persistence.swift @@ -32,6 +32,7 @@ class PersistenceController { let container: NSPersistentContainer init(inMemory: Bool = false) { + container = NSPersistentContainer(name: "Meshtastic") if inMemory { container.persistentStoreDescriptions.first!.url = URL(fileURLWithPath: "/dev/null") @@ -39,7 +40,7 @@ class PersistenceController { container.loadPersistentStores(completionHandler: { (_, error) in // Merge policy that favors in memory data over data in the db self.container.viewContext.mergePolicy = NSMergeByPropertyObjectTrumpMergePolicy - + self.container.viewContext.automaticallyMergesChangesFromParent = true if let error = error as NSError? { // Replace this implementation with code to handle the error appropriately. @@ -53,21 +54,22 @@ class PersistenceController { * The store could not be migrated to the current model version. Check the error message to determine what the actual problem was. */ - - let firstStoreURL = self.container.persistentStoreCoordinator.persistentStores.first?.url - - do { - - try self.container.persistentStoreCoordinator.destroyPersistentStore(at: firstStoreURL!, type: .sqlite, options: nil) - - print("💥 Something went terribly wrong, CoreData database truncated. All app data is lost.") - - } catch { - print("💣 Failed to destroy broken CoreData database, delete the app.") - } - - print("💣💥 Unresolved error \(error), \(error.userInfo)") + self.clearDatabase() } }) } + + public func clearDatabase() { + guard let url = self.container.persistentStoreDescriptions.first?.url else { return } + + let persistentStoreCoordinator = self.container.persistentStoreCoordinator + + do { + try persistentStoreCoordinator.destroyPersistentStore(at:url, ofType: NSSQLiteStoreType, options: nil) + print("💥 Something went terribly wrong, CoreData database truncated. All app data is lost.") + try persistentStoreCoordinator.addPersistentStore(ofType: NSSQLiteStoreType, configurationName: nil, at: url, options: nil) + } catch let error { + print("💣 Failed to destroy broken CoreData database, delete the app. Attempted to clear persistent store: " + error.localizedDescription) + } + } }