Created
August 7, 2015 23:04
-
-
Save grosch/871d5988705853d40f86 to your computer and use it in GitHub Desktop.
This file contains 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
// | |
// NSManagedObjectContext.swift | |
// TeamKnect | |
// | |
// Created by Scott Grosch on 8/7/15. | |
// Copyright (c) 2015 Gargoyle Software, LLC. All rights reserved. | |
// | |
import UIKit | |
import CoreData | |
extension NSManagedObjectContext { | |
class func errorToMessages(error: NSError) -> String { | |
return "\n".join(NSManagedObjectContext.errorToMessageSet(error)) | |
} | |
class func errorToMessageSet(error: NSError) -> Set<String> { | |
var set = Set<String>() | |
if error.domain != NSCocoaErrorDomain { | |
set.insert("\(error.localizedDescription)\n\(error.localizedFailureReason)") | |
} | |
let userInfo = error.userInfo | |
var attribute = userInfo[NSValidationKeyErrorKey] as? String | |
if attribute == nil { | |
attribute = "" | |
} | |
var msg = "" | |
switch error.code { | |
case NSManagedObjectValidationError: | |
msg = "Generic validation error" | |
case NSValidationDateTooLateError: | |
msg = "Date for '\(attribute!)' is newer than allowed." | |
case NSValidationDateTooSoonError: | |
msg = "Date for '\(attribute!)' is older than allowed." | |
case NSValidationInvalidDateError: | |
msg = "Date for '\(attribute!)' is invalid." | |
case NSValidationMissingMandatoryPropertyError: | |
msg = "Required attribute '\(attribute!)' is nil." | |
case NSValidationNumberTooLargeError: | |
msg = "Number '\(attribute!)' is larger than allowed." | |
case NSValidationNumberTooSmallError: | |
msg = "Number '\(attribute!)' is smaller than required.'" | |
case NSValidationRelationshipDeniedDeleteError: | |
msg = "Relationship '\(attribute!)' must be empty before deletion." | |
case NSValidationRelationshipExceedsMaximumCountError: | |
msg = "Relationship '\(attribute!)' has more than the maximum number of entries allowed." | |
case NSValidationRelationshipLacksMinimumCountError: | |
msg = "Relationship '\(attribute!)' has fewer than the minimum number of entries allowed." | |
case NSValidationStringTooLongError: | |
msg = "Text of '\(attribute!)' is longer than allowed." | |
case NSValidationStringTooShortError: | |
msg = "Text of '\(attribute!)' is shorter than required." | |
case NSValidationMultipleErrorsError: | |
if let subErrors = userInfo[NSDetailedErrorsKey] as? [NSError] { | |
for error in subErrors { | |
set.unionInPlace(NSManagedObjectContext.errorToMessageSet(error)) | |
} | |
} else { | |
return set | |
} | |
default: | |
msg = "Unknown reason, error code \(error.code)" | |
} | |
if let managedObject = userInfo[NSValidationObjectErrorKey] as? NSManagedObject { | |
if let entity = managedObject.entity.name { | |
msg = "\(entity): \(msg)" | |
} | |
} | |
set.insert(msg) | |
return set | |
} | |
/** | |
Save the managed object context. On failure, logs a message to the console and also, if in DEBUG | |
mode, pops up an alert with the message to make development easier. | |
:param: presentingViewController The view controller from which saveFromViewController was called | |
:returns: true on successful save, false on failure. | |
*/ | |
func saveFromViewController(presentingViewController presentingViewController: UIViewController? = nil) -> Bool { | |
if hasChanges { | |
do { | |
try save() | |
} catch { | |
let errorMessages = NSManagedObjectContext.errorToMessages(error as NSError) | |
print(errorMessages) | |
#if DEBUG | |
if let presentingViewController = presentingViewController { | |
dispatch_async(dispatch_get_main_queue()) { | |
let alert = UIAlertController(title: nil, message: errorMessages, preferredStyle: .Alert) | |
alert.addAction(UIAlertAction(title: "OK", style: .Default, handler: nil)) | |
presentingViewController.presentViewController(alert, animated: true, completion: nil) | |
} | |
} | |
#endif | |
} | |
} | |
return true | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment