I'm using Xcodes auto generated code for a core data swift project which includes this function in the app delegate:
func saveContext () {
if let moc = self.managedObjectContext {
var error: NSError? = nil
if moc.hasChanges && !moc.save(&error) {
// Replace this implementation with code to handle the error appropriately.
// abort() causes the application to generate a crash log and terminate. You should not use this function in a shipping application, although it may be useful during development.
NSLog("Unresolved error \(error), \(error!.userInfo)")
abort()
}
}
}
When I have an error with my managed objects instead of printing a message with details about the error the app crashes with an EXC_BAD_INSTRUCTION
error.
In the documentation it says:
A pointer to an NSError object. You do not need to create an NSError object. The save operation aborts after the first failure if you pass NULL.
Is the app aborting because error
is nil?
Is this a bug or is this expected behavior? (and if it's expected, what approach am I supposed to take to recover from the error instead of crashing?)
(I'm running Xcode 6.3.1)
Edit 1: error is at moc.save
function, not from abort()
- e.g. commenting out abort doesn't stop crash, and the NSLog is never reached.
Edit 2 adding screenshots of backtrace (?)
error
definition is fine. Callingabort()
makes the app terminate abnormally, as mentioned in the comments in that code. – Martin R