Meshtastic-Apple/Meshtastic/Persistence/Persistence.swift

88 lines
3 KiB
Swift
Raw Normal View History

2021-11-29 19:58:06 -08:00
//
// Persistence.swift
// Meshtastic
2021-11-29 19:58:06 -08:00
//
// Copyright(c) Garth Vander Houwen 11/28/21.
2021-11-29 19:58:06 -08:00
//
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) {
2023-03-06 10:33:18 -08:00
2021-12-12 17:17:46 -08:00
container = NSPersistentContainer(name: "Meshtastic")
2023-03-06 10:33:18 -08:00
2021-12-12 17:17:46 -08:00
if inMemory {
container.persistentStoreDescriptions.first!.url = URL(fileURLWithPath: "/dev/null")
}
2023-03-06 10:33:18 -08:00
2021-12-25 23:48:12 -08:00
container.loadPersistentStores(completionHandler: { (_, error) in
2023-03-06 10:33:18 -08:00
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
2023-03-06 10:33:18 -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
}
})
}
2023-03-06 10:33:18 -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 {
2023-03-06 10:33:18 -08:00
try persistentStoreCoordinator.destroyPersistentStore(at: url, ofType: NSSQLiteStoreType, options: nil)
2021-12-29 16:13:17 -08:00
try persistentStoreCoordinator.addPersistentStore(ofType: NSSQLiteStoreType, configurationName: nil, at: url, options: nil)
print("💥 CoreData database truncated. All app data has been erased.")
2023-03-06 10:33:18 -08:00
2021-12-29 16:13:17 -08:00
} catch let error {
print("💣 Failed to destroy CoreData database, delete the app and re-install to clear data. Attempted to clear persistent store: " + error.localizedDescription)
2021-12-29 16:13:17 -08:00
}
}
2021-11-29 19:58:06 -08:00
}
extension NSManagedObjectContext {
2023-03-06 10:33:18 -08:00
/// Executes the given `NSBatchDeleteRequest` and directly merges the changes to bring the given managed object context up to date.
///
/// - Parameter batchDeleteRequest: The `NSBatchDeleteRequest` to execute.
/// - Throws: An error if anything went wrong executing the batch deletion.
public func executeAndMergeChanges(using batchDeleteRequest: NSBatchDeleteRequest) throws {
batchDeleteRequest.resultType = .resultTypeObjectIDs
2023-03-06 10:33:18 -08:00
let result = try execute(batchDeleteRequest) as? NSBatchDeleteResult
let changes: [AnyHashable: Any] = [NSDeletedObjectsKey: result?.result as? [NSManagedObjectID] ?? []]
2023-03-06 10:33:18 -08:00
NSManagedObjectContext.mergeChanges(fromRemoteContextSave: changes, into: [self])
}
}