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
}
} 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()
}

View File

@ -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)")
}
}
}

View File

@ -1,7 +1,6 @@
import Foundation
import CoreData
extension 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>
<dict>
<key>orderHint</key>
<integer>1</integer>
<integer>0</integer>
</dict>
<key>WriteFreely-MultiPlatform (macOS).xcscheme_^#shared#^_</key>
<dict>
<key>orderHint</key>
<integer>0</integer>
<integer>1</integer>
</dict>
</dict>
</dict>