Apologies if this is a bit vague, but that's half of the problem.
I have a document based core data app that I am working on, it is doing what it's told while running and doesn't generate any errors. But when the user saves the document, the document pops up a 'The document "xyz" could not be saved as "xyz". Multiple validation errors occurred' alert.
My question is - where do you start looking to fix / debug this? As the program does not fall over in the debugger I have no stack trace etc. Is this liable to be a faulty Entity relationship, or no data being saved in a non-optional Attribute of an Entity or... Is there a way to tell exactly what is failing validation?
Any suggestions of the best way to开发者_JAVA技巧 proceed greatly appreciated.
Related to this, what / how is the best method to catch such an error in the future so it doesn't get as far as the user.
Many Thanks
Ok, as TechZen suggested, capture the error from the save operation. Add the following to MyDocument.m
- (NSError *)willPresentError:(NSError *)error {
// Only deal with Core Data Errors
if (!([[error domain] isEqualToString:NSCocoaErrorDomain])) {
return error;
}
NSInteger errorCode = [error code];
if ((errorCode < NSValidationErrorMinimum) || (errorCode > NSValidationErrorMaximum)) {
return error;
}
// If there is only 1 error, let the usual alert display it
if (errorCode != NSValidationMultipleErrorsError) {
return error;
}
// Get the errors. NSValidationMultipleErrorsError - the errors are in an array in the userInfo dictionary for key NSDetailedErrorsKey
NSArray *detailedErrors = [[error userInfo] objectForKey:NSDetailedErrorsKey];
NSUInteger errorCount = [detailedErrors count];
NSMutableString *errorString = [NSMutableString stringWithFormat:@"There are %lu validation errors:-", errorCount];
for (int i = 0; i < errorCount; i++) {
[errorString appendFormat:@"%@\n",
[[detailedErrors objectAtIndex:i] localizedDescription]];
}
// Create a new error with the new userInfo and return it
NSMutableDictionary *newUserInfo = [NSMutableDictionary dictionaryWithDictionary:[error userInfo]];
[newUserInfo setObject:errorString forKey:NSLocalizedDescriptionKey];
NSError *newError = [NSError errorWithDomain:[error domain] code:[error code] userInfo:newUserInfo];
return newError;
}
Note if there are 100 errors then you will get an alert with 100 items in which is not the best, but this is a good starting point for dealing with errors on save.
Validation errors suggest that the problem lays with validation predicates that are applied when the document is saved. In turn that means that some data you've attempted to save is of the wrong type or the wrong values.
If you capture the error return from the save operation, the userInfo dictionary should contain details about the failures.
the usual suspect is a property (or two) not set to OPTIONAL
, yet with no value.
so offer a category for awakeFromInsert
that is called only once in NSManagedObject's
lifetime.
@implementation Entity (Entity_Category)
- (void) awakeFromInsert
{
[super awakeFromInsert];
[self setPrimitiveValue:[NSDate date] forKey:@"dateCreate"];
[self setPrimitiveValue:[NSDate date] forKey:@"dateUpdate"];
}
- Use setPrimitiveValue to avoid avoid recording as an UNDO
If you set any regex validation in the core data property, such error will appear too. Maybe you can check on this area. I got this error by this way.
精彩评论