Meshtastic-Apple/Meshtastic/Persistence/Persistence.swift

108 lines
3.7 KiB
Swift
Raw Permalink 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
2024-06-03 02:17:55 -07:00
import OSLog
2021-11-29 19:58:06 -08:00
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
2025-04-24 09:50:59 -07:00
self.container.viewContext.retainsRegisteredObjects = 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
2025-03-31 22:06:00 -07:00
Logger.data.error("CoreData Error: \(error.localizedDescription, privacy: .public). 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)
2024-06-03 02:17:55 -07:00
Logger.data.error("CoreData database truncated. All app data has been erased.")
do {
try persistentStoreCoordinator.addPersistentStore(ofType: NSSQLiteStoreType, configurationName: nil, at: url, options: nil)
} catch let error {
2025-03-31 22:06:00 -07:00
Logger.data.error("Failed to re-create CoreData database: \(error.localizedDescription, privacy: .public)")
}
2023-03-06 10:33:18 -08:00
2021-12-29 16:13:17 -08:00
} catch let error {
2025-03-31 22:06:00 -07:00
Logger.data.error("Failed to destroy CoreData database, delete the app and re-install to clear data. Attempted to clear persistent store: \(error.localizedDescription, privacy: .public)")
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])
}
}
2024-06-23 13:00:20 -07:00
// Created by Tom Harrington on 5/12/20.
// Copyright © 2020 Atomic Bird LLC. All rights reserved.
// Gist from https://atomicbird.com/blog/core-data-back-up-store/
//
extension NSPersistentContainer {
enum CopyPersistentStoreErrors: Error {
case invalidDestination(String)
case destinationError(String)
case destinationNotRemoved(String)
case copyStoreError(String)
case invalidSource(String)
}
2024-06-23 16:11:02 -07:00
2024-06-23 13:00:20 -07:00
}