Meshtastic-Apple/MeshtasticClient/Persistence/Persistence.swift

73 lines
2.3 KiB
Swift
Raw Normal View History

2021-11-29 19:58:06 -08:00
//
// Persistence.swift
// CoreDataSample
//
// Created by Garth Vander Houwen on 11/28/21.
//
import CoreData
2021-12-12 17:17:46 -08:00
class PersistenceController {
2021-12-25 23:48:12 -08:00
2021-12-12 17:17:46 -08:00
static let shared = PersistenceController()
2021-11-29 19:58:06 -08:00
2021-12-12 17:17:46 -08:00
static var preview: PersistenceController = {
let result = PersistenceController(inMemory: false)
let viewContext = result.container.viewContext
for _ in 0..<10 {
let newItem = NodeInfoEntity(context: viewContext)
newItem.lastHeard = Date()
2021-12-12 17:17:46 -08:00
}
do {
try viewContext.save()
} catch {
// Replace this implementation with code to handle the error appropriately.
// fatalError() causes the application to generate a crash log and terminate. You should not use this function in a shipping application, although it may be useful during development.
let nsError = error as NSError
fatalError("Unresolved error \(nsError), \(nsError.userInfo)")
}
return result
}()
2021-11-29 19:58:06 -08:00
2021-12-12 17:17:46 -08:00
let container: NSPersistentContainer
2021-11-29 19:58:06 -08:00
2021-12-12 17:17:46 -08:00
init(inMemory: Bool = false) {
2021-12-29 16:13:17 -08:00
2021-12-12 17:17:46 -08:00
container = NSPersistentContainer(name: "Meshtastic")
2022-05-05 18:01:20 -07:00
//self.clearDatabase()
2022-01-01 08:03:46 -08:00
2021-12-12 17:17:46 -08:00
if inMemory {
container.persistentStoreDescriptions.first!.url = URL(fileURLWithPath: "/dev/null")
}
2022-01-01 08:03:46 -08:00
2021-12-25 23:48:12 -08:00
container.loadPersistentStores(completionHandler: { (_, error) in
2021-12-12 17:17:46 -08:00
// Merge policy that favors in memory data over data in the db
self.container.viewContext.mergePolicy = NSMergeByPropertyObjectTrumpMergePolicy
2022-01-01 08:03:46 -08:00
self.container.viewContext.automaticallyMergesChangesFromParent = true
2021-12-28 09:11:24 -08:00
2021-12-12 17:17:46 -08:00
if let error = error as NSError? {
2021-11-29 19:58:06 -08:00
2021-12-31 20:52:22 -08:00
print("💥 CoreData Error: \(error.localizedDescription). Now attempting to truncate CoreData database. All app data will be lost.")
2021-12-29 16:13:17 -08:00
self.clearDatabase()
2021-12-12 17:17:46 -08:00
}
})
}
2021-12-29 16:13:17 -08:00
public func clearDatabase() {
guard let url = self.container.persistentStoreDescriptions.first?.url else { return }
let persistentStoreCoordinator = self.container.persistentStoreCoordinator
do {
2021-12-29 16:13:17 -08:00
try persistentStoreCoordinator.destroyPersistentStore(at:url, ofType: NSSQLiteStoreType, options: nil)
try persistentStoreCoordinator.addPersistentStore(ofType: NSSQLiteStoreType, configurationName: nil, at: url, options: nil)
print("💥 Something went terribly wrong, CoreData database truncated. All app data is lost.")
2021-12-29 16:13:17 -08:00
} catch let error {
print("💣 Failed to destroy broken CoreData database, delete the app. Attempted to clear persistent store: " + error.localizedDescription)
}
}
2021-11-29 19:58:06 -08:00
}