1

Trying to find out Core Data Errors Error, and looking inside userInfo in NSError returned by MOC's save method.

iphone-core-data-unresolved-error-while-saving I am not able to make commends or raise questions due to my lack of points.

Can any one point out for me where following keys are defined in Apple Doc, NSValidationErrorObject, NSValidationErrorKey...etc?

My understanding is: if CoreData's Error code == NSValidationMultipleErrorsError, then NSError's userInfo(dictionary) will contain all detailed-single-error information, and all those detailed-single-error will be accessible from an Array stored under "NSDetailedErrorsKey" from userInfo.

I just don't see them (NSValidationErrorObject, NSValidationErrorKe...) in Core Data Constants Reference...??? Are they opaque ?

Thanks.


- (BOOL)saveData {
    NSError *error;
    if (![_sharedManagedObjectContext save:&error]) {
        // If Cocoa generated the error...
        NSString *message = nil;
        if ([[error domain] isEqualToString:@"NSCocoaErrorDomain"]) {
            // ...check whether there's an NSDetailedErrors array            
            NSDictionary *userInfo = [error userInfo];
            if ([userInfo valueForKey:@"NSDetailedErrors"] != nil) {
                // ...and loop through the array, if so.
                NSArray *errors = [userInfo valueForKey:@"NSDetailedErrors"];
                for (NSError *anError in errors) {

                    NSDictionary *subUserInfo = [anError userInfo];
                    subUserInfo = [anError userInfo];
                    // Granted, this indents the NSValidation keys rather a lot
                    // ...but it's a small loss to keep the code more readable.
                    NSLog(@"Core Data Save Error\n\n \
                      NSValidationErrorKey\n%@\n\n \
                      NSValidationErrorPredicate\n%@\n\n \
                      NSValidationErrorObject\n%@\n\n \
                      NSLocalizedDescription\n%@", 
                      [subUserInfo valueForKey:@"NSValidationErrorKey"], 
                      [subUserInfo valueForKey:@"NSValidationErrorPredicate"], 
                      [subUserInfo valueForKey:@"NSValidationErrorObject"], 
                      [subUserInfo valueForKey:@"NSLocalizedDescription"]);
                }
            }
            // If there was no NSDetailedErrors array, print values directly
            // from the top-level userInfo object. (Hint: all of these keys
            // will have null values when you've got multiple errors sitting
            // behind the NSDetailedErrors key.
            else {
                    NSLog(@"Core Data Save Error\n\n \
                      NSValidationErrorKey\n%@\n\n \
                      NSValidationErrorPredicate\n%@\n\n \
                      NSValidationErrorObject\n%@\n\n \
                      NSLocalizedDescription\n%@", 
                      [userInfo valueForKey:@"NSValidationErrorKey"], 
                      [userInfo valueForKey:@"NSValidationErrorPredicate"], 
                      [userInfo valueForKey:@"NSValidationErrorObject"], 
                      [userInfo valueForKey:@"NSLocalizedDescription"]);

            }
        } 
        // Handle mine--or 3rd party-generated--errors
        else {
            NSLog(@"Custom Error: %@", [error localizedDescription]);
        }
        return NO;
    }
    return YES;
}


  [1]: https://stackoverflow.com/questions/1283960/iphone-core-data-unresolved-error-while-saving
Community
  • 1
  • 1
J-Q
  • 276
  • 5
  • 15

1 Answers1

0

Following is the quoted from @quellish message, thanks @quellish!

Core Data validation errors are defined in CoreDataErrors.h. I am considering asking Apple to move that stuff out of core data and into Foundation, since Key Value Coding really "owns" validation. Validation errors should always have a errorCode value between NSValidationErrorMinimum and NSValidationErrorMaximum as well. I have an example validation error that may be helpful to you.

Single validation error (only one property failed): Error Domain=NSCocoaErrorDomain Code=1560 "The operation couldn’t be completed. (Cocoa error 1560.)" UserInfo=0x2808ac0 {NSDetailedErrors=( 
"Error Domain=NSCocoaErrorDomain Code=1670 \"Name must be John\" UserInfo=0x2805f30 {NSLocalizedDescription=Name must be John}", 
"Error Domain=NSCocoaErrorDomain Code=1570 \"The operation couldn\U2019t be completed. (Cocoa error 1570.)\" UserInfo=0x28089c0 {NSValidationErrorObject=<Employee: 0xc003180> (entity: Employee; id: 0xc0031d0 <x-coredata:///Employee/t6FEF17D8-0306-4959-9BFB-4B806E6ED1302> ; da…

see full text

a multiple looks similar, with additional NSErrors in the array that is the value of the userInfo dictionary for the key NSDetailedErrorsKey. Note that the top level NSError is a single error with the cocoa error domain and error code 1560. That is NSValidationMultipleErrorsError. That error should always contain additional errors under the NSDetailedErrorsKey. hope that helps!

Community
  • 1
  • 1
J-Q
  • 276
  • 5
  • 15