Skip to content

Instantly share code, notes, and snippets.

@mnbayan
Created October 17, 2015 13:29
Show Gist options
  • Save mnbayan/3bc022c80f19fd76149a to your computer and use it in GitHub Desktop.
Save mnbayan/3bc022c80f19fd76149a to your computer and use it in GitHub Desktop.
NSManagedObjectContext extension that handles saving. Logs all errors if there are any.
import Foundation
import CoreData
extension NSManagedObjectContext{
func saveContext(){
if self.hasChanges {
do{
try self.save()
}
catch let error as NSError{
let errorInfo = error.userInfo as NSDictionary!
if let detailedErrors = errorInfo[NSDetailedErrorsKey] as? [NSError]{
if detailedErrors.count > 0{
for i in 0..<detailedErrors.count{
print("\(__FUNCTION__) Error[\(i)]: \(detailedErrors[i].localizedDescription)")
}
}
else{
print("\(__FUNCTION__) Error: \(detailedErrors[0].localizedDescription)")
}
}
else{
print("\(__FUNCTION__), Could not save: \(error.localizedDescription)")
}
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment