Ty-wan Claxton
Ty-wan Claxton

Reputation: 49

IOS -Core Data Stacl

My question are as follows:

  1. Will creating a custom enum to handle the coredata errors be the best way to handle errors in this case
  2. If the persistent store container doesn't load or we can't save the context then there is no need to use the app and crashing is the best option correct?
  3. In order to present these error messages to the user, wouldn't I have to adapt UIAlertAction? Which would also mean I would need to register notifications?
  4. By law we have to get permission to send notifications, would it be best to create a whole new file for notification & add the error enum in that file or would it make more sense to have the core data stack class conform to the Notification protocol?

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

Answers (2)

Rob Napier
Rob Napier

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

Joakim Danielson
Joakim Danielson

Reputation: 52023

  1. That's up to you.

  2. Exit the app properly with an alert and not by crashing it

  3. Why would you need notifications for showing an alert?

  4. This question makes no sense to me.

Upvotes: 0

Related Questions