Add saveContext method to PersistenceManager, clean up warnings

This commit is contained in:
Angelo Stavrow 2020-09-02 10:36:28 -04:00
parent 2bb47845a5
commit 09d1d76d47
No known key found for this signature in database
GPG Key ID: 1A49C7064E060EEE
4 changed files with 23 additions and 12 deletions

View File

@ -133,7 +133,8 @@ private extension WriteFreelyModel {
self.account.currentError = AccountError.invalidPassword self.account.currentError = AccountError.invalidPassword
} }
} catch { } 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 { DispatchQueue.main.async {
self.account.currentError = AccountError.serverNotFound 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 // 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. // 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. // Conditional cast from 'Error' to 'NSError' always succeeds but is the only way to check error properties.
if let error = error as? NSError, if (error as NSError).domain == NSURLErrorDomain,
error.domain == NSURLErrorDomain, (error as NSError).code == NSURLErrorCannotParseResponse {
error.code == NSURLErrorCannotParseResponse {
if account.isLoggedIn { if account.isLoggedIn {
self.logout() self.logout()
} }

View File

@ -8,8 +8,8 @@ class PersistenceManager {
let persistentContainer: NSPersistentContainer = { let persistentContainer: NSPersistentContainer = {
let container = NSPersistentContainer(name: "LocalStorageModel") let container = NSPersistentContainer(name: "LocalStorageModel")
container.loadPersistentStores(completionHandler: { (_, error) in container.loadPersistentStores(completionHandler: { (_, error) in
if let error = error as NSError? { if let error = error {
fatalError("Unresolved error loading persistent store: \(error) - \(error.userInfo)") fatalError("Unresolved error loading persistent store: \(error)")
} }
}) })
return container return container
@ -24,11 +24,23 @@ class PersistenceManager {
let notification = NSApplication.willResignActiveNotification let notification = NSApplication.willResignActiveNotification
#endif #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 center.addObserver(forName: notification, object: nil, queue: nil) { [weak self] _ in
guard let self = self else { return } guard let self = self else { return }
self.saveContext()
}
}
if self.persistentContainer.viewContext.hasChanges { func saveContext() {
try? self.persistentContainer.viewContext.save() if persistentContainer.viewContext.hasChanges {
do {
try persistentContainer.viewContext.save()
} catch {
print("Error saving context: \(error)")
} }
} }
} }

View File

@ -1,7 +1,6 @@
import Foundation import Foundation
import CoreData import CoreData
extension WFACollection { extension WFACollection {
@nonobjc public class func fetchRequest() -> NSFetchRequest<WFACollection> { @nonobjc public class func fetchRequest() -> NSFetchRequest<WFACollection> {
@ -19,6 +18,6 @@ extension WFACollection {
} }
extension WFACollection : Identifiable { extension WFACollection: Identifiable {
} }

View File

@ -7,12 +7,12 @@
<key>WriteFreely-MultiPlatform (iOS).xcscheme_^#shared#^_</key> <key>WriteFreely-MultiPlatform (iOS).xcscheme_^#shared#^_</key>
<dict> <dict>
<key>orderHint</key> <key>orderHint</key>
<integer>1</integer> <integer>0</integer>
</dict> </dict>
<key>WriteFreely-MultiPlatform (macOS).xcscheme_^#shared#^_</key> <key>WriteFreely-MultiPlatform (macOS).xcscheme_^#shared#^_</key>
<dict> <dict>
<key>orderHint</key> <key>orderHint</key>
<integer>0</integer> <integer>1</integer>
</dict> </dict>
</dict> </dict>
</dict> </dict>