Reputation: 49
My question are as follows:
The end goal is to notify the user that either the persistent store wouldn't load or the moc wouldn't save
Thanks for your time in advance!
enum CoreDataError: Error {
// This is my custom Error handling enum
case persistenStore(description: String )
case saveChanges(description: String )
}
func coreDataErrors(throwError: Bool) throws -> CoreDataError {
// This is my Error Handling function
}
class CoreDataStack {
lazy var managedObjectContext: NSManagedObjectContext = {
let container = self.persistentContainer
return container.viewContext
}()
private lazy var persistentContainer: NSPersistentContainer = {
let container = NSPersistentContainer(name: "SafeHouseCDPhotoVault")
container.loadPersistentStores() { storeDescription, error in
if let error = error as NSError? {
fatalError("Unresolved error: \(error), \(error.userInfo)")
}
}
return container
}()
}
/* In almost every app tutorial available we learn to deal with errors using this fatal error logic to speed through the project
*/
extension NSManagedObjectContext {
func saveChanges() {
if self.hasChanges {
do {
try save()
} catch {
fatalError("Error: \(error.localizedDescription)")
}
}
}
}
// Again the demos have us deal with errors using fatal error
Upvotes: 0
Views: 82
Reputation: 299565
I typically use enums for error cases, but I wouldn't name them according to their source. Name them according to how you will will them for recovery or error-handler behavior. If you can't handle them, there's not much point in generating them.
That gets to the second question; if you can't handle a case at all (such as the MOM missing on launch), crashing is about all you can do. I don't like alerts in that case. What's the user going to do with that information? A crash at least will be sent to Apple and you can see it and do something about it.
If the user can do something, then absolutely provide an error. If there is any hope that the error is transitory (such as a save failure, which may be due to a full disk), then maybe provide them an error/retry. But on iOS this generally is not worth the trouble and the risk of generating bugs. How are you going to test your error/retry system? If you can't test it, how do you know it's better than crashing? (This isn't an idle question; I once built a crash-catching system that had a bug and caused the crash handler to go into a tight loop and drain the battery rapidly. That's worse than crashing.)
If you're a beginner, then you're probably not in a place to handle uncommon Core Data errors and the best and safest thing you're going to do is crash. Handling these things well is quite complex and difficult to test, and I generally do not recommend it on iOS (macOS is a bit different because write-errors are much more often transitory).
Upvotes: 1
Reputation: 52023
That's up to you.
Exit the app properly with an alert and not by crashing it
Why would you need notifications for showing an alert?
This question makes no sense to me.
Upvotes: 0