Created
October 17, 2015 13:29
-
-
Save mnbayan/3bc022c80f19fd76149a to your computer and use it in GitHub Desktop.
NSManagedObjectContext extension that handles saving. Logs all errors if there are any.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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