文档“xyz”无法保存。发生多个验证错误

发布于 2024-11-06 00:07:08 字数 339 浏览 0 评论 0原文

如果这有点含糊,我很抱歉,但这只是问题的一半。

我正在开发一个基于文档的核心数据应用程序,它在运行时按照指示执行操作,并且不会生成任何错误。但是当用户保存文档时,文档弹出“文档“xyz”无法保存为“xyz””。发生多个验证错误'警报。

我的问题是 - 你从哪里开始修复/调试这个问题?由于程序不会在调试器中失败,所以我没有堆栈跟踪等。这是否可能是错误的实体关系,或者没有数据保存在实体的非可选属性中,或者...有没有办法准确说出验证失败的原因?

任何关于最佳进行方式的建议都非常感谢。

与此相关的是,将来捕获此类错误的最佳方法是什么/如何,这样用户就不会受到这种错误的影响。

非常感谢

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 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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

ら栖息 2024-11-13 00:07:08

好的,按照 TechZen 的建议,捕获保存操作中的错误。将以下内容添加到 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;
}

注意,如果有 100 个错误,那么您将收到包含 100 个项目的警报,其中不是最好的,但这是处理保存错误的一个很好的起点。

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.

丿*梦醉红颜 2024-11-13 00:07:08

验证错误表明问题在于保存文档时应用的验证谓词。反过来,这意味着您尝试保存的某些数据的类型或值错误。

如果您捕获保存操作返回的错误,则 userInfo 字典应包含有关失败的详细信息。

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.

鹿港小镇 2024-11-13 00:07:08

通常的嫌疑是一个(或两个)属性未设置为“OPTIONAL”,但没有任何值。
因此,为awakeFromInsert提供一个类别,该类别在NSManagedObject的生命周期中仅调用一次。

@implementation Entity (Entity_Category)

- (void) awakeFromInsert
{
[super awakeFromInsert];    

[self setPrimitiveValue:[NSDate date] forKey:@"dateCreate"];
[self setPrimitiveValue:[NSDate date] forKey:@"dateUpdate"];
}   
  • 使用 setPrimitiveValue 避免记录为 UNDO

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
提赋 2024-11-13 00:07:08

如果您在核心数据属性中设置任何正则表达式验证,也会出现此类错误。也许你可以检查一下这个区域。我通过这种方式得到了这个错误。

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.

み零 2024-11-13 00:07:08

原因之一可能是,如果您尝试保存属性为空值的实体,而该属性配置为非可选。一种简单的检查方法是在 saveContext 上设置断点并打印 NSManagedObject 上下文的createdObjects/insertedObjects 属性的结果。由于 NULL 在 Core Data 和 Swift 中的含义略有不同,因此这是一个非常常见的错误。

您可以查看 Tom Harrington 的答案:https://stackoverflow.com/a/33552046/5193436

One reason could be if you are trying to save an entity with a null value on a property, which is configured to be non-optional. One easy way to check is to set a breakpoint on the saveContext and print the result for createdObjects/insertedObjects properties of the NSManagedObject context. Since NULL has a slightly different meaning in Core Data and Swift, this is a very common mistake to make.

You can check this answer by Tom Harrington: https://stackoverflow.com/a/33552046/5193436

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文