From 09d1d76d471505fa829f93b4fc08eabe7d10774b Mon Sep 17 00:00:00 2001 From: Angelo Stavrow Date: Wed, 2 Sep 2020 10:36:28 -0400 Subject: [PATCH] Add saveContext method to PersistenceManager, clean up warnings --- Shared/Models/WriteFreelyModel.swift | 8 ++++---- Shared/PersistenceManager.swift | 20 +++++++++++++++---- WFACollection+CoreDataProperties.swift | 3 +-- .../xcschemes/xcschememanagement.plist | 4 ++-- 4 files changed, 23 insertions(+), 12 deletions(-) diff --git a/Shared/Models/WriteFreelyModel.swift b/Shared/Models/WriteFreelyModel.swift index b70aeb9..9e6e8b8 100644 --- a/Shared/Models/WriteFreelyModel.swift +++ b/Shared/Models/WriteFreelyModel.swift @@ -133,7 +133,8 @@ private extension WriteFreelyModel { self.account.currentError = AccountError.invalidPassword } } catch { - if let error = error as? NSError, error.domain == NSURLErrorDomain, error.code == -1003 { + if (error as NSError).domain == NSURLErrorDomain, + (error as NSError).code == -1003 { DispatchQueue.main.async { self.account.currentError = AccountError.serverNotFound } @@ -175,9 +176,8 @@ private extension WriteFreelyModel { // so we're using a hacky workaround — if we get the NSURLError, but the AccountModel still thinks we're // logged in, try calling the logout function again and see what we get. // Conditional cast from 'Error' to 'NSError' always succeeds but is the only way to check error properties. - if let error = error as? NSError, - error.domain == NSURLErrorDomain, - error.code == NSURLErrorCannotParseResponse { + if (error as NSError).domain == NSURLErrorDomain, + (error as NSError).code == NSURLErrorCannotParseResponse { if account.isLoggedIn { self.logout() } diff --git a/Shared/PersistenceManager.swift b/Shared/PersistenceManager.swift index e75aed1..fd69f13 100644 --- a/Shared/PersistenceManager.swift +++ b/Shared/PersistenceManager.swift @@ -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)") } } } diff --git a/WFACollection+CoreDataProperties.swift b/WFACollection+CoreDataProperties.swift index dea6ea7..07c7271 100644 --- a/WFACollection+CoreDataProperties.swift +++ b/WFACollection+CoreDataProperties.swift @@ -1,7 +1,6 @@ import Foundation import CoreData - extension WFACollection { @nonobjc public class func fetchRequest() -> NSFetchRequest { @@ -19,6 +18,6 @@ extension WFACollection { } -extension WFACollection : Identifiable { +extension WFACollection: Identifiable { } diff --git a/WriteFreely-MultiPlatform.xcodeproj/xcuserdata/angelo.xcuserdatad/xcschemes/xcschememanagement.plist b/WriteFreely-MultiPlatform.xcodeproj/xcuserdata/angelo.xcuserdatad/xcschemes/xcschememanagement.plist index 6cd8075..2723ebe 100644 --- a/WriteFreely-MultiPlatform.xcodeproj/xcuserdata/angelo.xcuserdatad/xcschemes/xcschememanagement.plist +++ b/WriteFreely-MultiPlatform.xcodeproj/xcuserdata/angelo.xcuserdatad/xcschemes/xcschememanagement.plist @@ -7,12 +7,12 @@ WriteFreely-MultiPlatform (iOS).xcscheme_^#shared#^_ orderHint - 1 + 0 WriteFreely-MultiPlatform (macOS).xcscheme_^#shared#^_ orderHint - 0 + 1