Meshtastic-Apple/MeshtasticClient/Model/Data/PersistanceController.swift
2021-11-02 21:47:41 -07:00

36 lines
768 B
Swift

import CoreData
struct PersistenceController {
static let shared = PersistenceController()
let container: NSPersistentContainer
init() {
container = NSPersistentContainer(name: "Mesh")
container.loadPersistentStores { (description, error ) in
if let error = error {
fatalError("Error: \(error.localizedDescription)")
}
}
}
func save(completion: @escaping (Error?) -> () = {_ in}) {
let context = container.viewContext
if context.hasChanges {
do {
try context.save()
completion(nil)
} catch {
completion(error)
}
}
}
func delete(_ object: NSManagedObject, completion: @escaping (Error?) -> () = { _
in }) {
let context = container.viewContext
context.delete(object)
save(completion: completion)
}
}