iPhone Core Data “Production” Error Handling

前端 未结 5 1119
陌清茗
陌清茗 2020-12-04 05:52

I\'ve seen in the example code supplied by Apple references to how you should handle Core Data errors. I.e:

NSError *error = nil;
if (![context save:&err         


        
5条回答
  •  天命终不由人
    2020-12-04 06:11

    This is one generic method I came up with to handle and display validation errors on the iPhone. But Marcus is right: You'd probably want to tweak the messages to be more user friendly. But this at least gives you a starting point to see what field didn't validate and why.

    - (void)displayValidationError:(NSError *)anError {
        if (anError && [[anError domain] isEqualToString:@"NSCocoaErrorDomain"]) {
            NSArray *errors = nil;
    
            // multiple errors?
            if ([anError code] == NSValidationMultipleErrorsError) {
                errors = [[anError userInfo] objectForKey:NSDetailedErrorsKey];
            } else {
                errors = [NSArray arrayWithObject:anError];
            }
    
            if (errors && [errors count] > 0) {
                NSString *messages = @"Reason(s):\n";
    
                for (NSError * error in errors) {
                    NSString *entityName = [[[[error userInfo] objectForKey:@"NSValidationErrorObject"] entity] name];
                    NSString *attributeName = [[error userInfo] objectForKey:@"NSValidationErrorKey"];
                    NSString *msg;
                    switch ([error code]) {
                        case NSManagedObjectValidationError:
                            msg = @"Generic validation error.";
                            break;
                        case NSValidationMissingMandatoryPropertyError:
                            msg = [NSString stringWithFormat:@"The attribute '%@' mustn't be empty.", attributeName];
                            break;
                        case NSValidationRelationshipLacksMinimumCountError:  
                            msg = [NSString stringWithFormat:@"The relationship '%@' doesn't have enough entries.", attributeName];
                            break;
                        case NSValidationRelationshipExceedsMaximumCountError:
                            msg = [NSString stringWithFormat:@"The relationship '%@' has too many entries.", attributeName];
                            break;
                        case NSValidationRelationshipDeniedDeleteError:
                            msg = [NSString stringWithFormat:@"To delete, the relationship '%@' must be empty.", attributeName];
                            break;
                        case NSValidationNumberTooLargeError:                 
                            msg = [NSString stringWithFormat:@"The number of the attribute '%@' is too large.", attributeName];
                            break;
                        case NSValidationNumberTooSmallError:                 
                            msg = [NSString stringWithFormat:@"The number of the attribute '%@' is too small.", attributeName];
                            break;
                        case NSValidationDateTooLateError:                    
                            msg = [NSString stringWithFormat:@"The date of the attribute '%@' is too late.", attributeName];
                            break;
                        case NSValidationDateTooSoonError:                    
                            msg = [NSString stringWithFormat:@"The date of the attribute '%@' is too soon.", attributeName];
                            break;
                        case NSValidationInvalidDateError:                    
                            msg = [NSString stringWithFormat:@"The date of the attribute '%@' is invalid.", attributeName];
                            break;
                        case NSValidationStringTooLongError:      
                            msg = [NSString stringWithFormat:@"The text of the attribute '%@' is too long.", attributeName];
                            break;
                        case NSValidationStringTooShortError:                 
                            msg = [NSString stringWithFormat:@"The text of the attribute '%@' is too short.", attributeName];
                            break;
                        case NSValidationStringPatternMatchingError:          
                            msg = [NSString stringWithFormat:@"The text of the attribute '%@' doesn't match the required pattern.", attributeName];
                            break;
                        default:
                            msg = [NSString stringWithFormat:@"Unknown error (code %i).", [error code]];
                            break;
                    }
    
                    messages = [messages stringByAppendingFormat:@"%@%@%@\n", (entityName?:@""),(entityName?@": ":@""),msg];
                }
                UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Validation Error" 
                                                                message:messages
                                                               delegate:nil 
                                                      cancelButtonTitle:nil otherButtonTitles:@"OK", nil];
                [alert show];
                [alert release];
            }
        }
    }
    

    Enjoy.

提交回复
热议问题