|
|
@@ -8,8 +8,8 @@ class PersistenceManager { |
|
|
|
let persistentContainer: NSPersistentContainer = { |
|
|
|
let container = NSPersistentContainer(name: "LocalStorageModel") |
|
|
|
container.loadPersistentStores(completionHandler: { (_, error) in |
|
|
|
if let error = error as NSError? { |
|
|
|
fatalError("Unresolved error loading persistent store: \(error) - \(error.userInfo)") |
|
|
|
if let error = error { |
|
|
|
fatalError("Unresolved error loading persistent store: \(error)") |
|
|
|
} |
|
|
|
}) |
|
|
|
return container |
|
|
@@ -24,11 +24,23 @@ class PersistenceManager { |
|
|
|
let notification = NSApplication.willResignActiveNotification |
|
|
|
#endif |
|
|
|
|
|
|
|
// We don't need to worry about removing this observer because we're targeting iOS 9+ / macOS 10.11+; the |
|
|
|
// system will clean this up the next time it would be posted to. |
|
|
|
// See: https://developer.apple.com/documentation/foundation/notificationcenter/1413994-removeobserver |
|
|
|
// And: https://developer.apple.com/documentation/foundation/notificationcenter/1407263-removeobserver |
|
|
|
// swiftlint:disable:next discarded_notification_center_observer |
|
|
|
center.addObserver(forName: notification, object: nil, queue: nil) { [weak self] _ in |
|
|
|
guard let self = self else { return } |
|
|
|
self.saveContext() |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
if self.persistentContainer.viewContext.hasChanges { |
|
|
|
try? self.persistentContainer.viewContext.save() |
|
|
|
func saveContext() { |
|
|
|
if persistentContainer.viewContext.hasChanges { |
|
|
|
do { |
|
|
|
try persistentContainer.viewContext.save() |
|
|
|
} catch { |
|
|
|
print("Error saving context: \(error)") |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|