文档“xyz”无法保存。发生多个验证错误
如果这有点含糊,我很抱歉,但这只是问题的一半。
我正在开发一个基于文档的核心数据应用程序,它在运行时按照指示执行操作,并且不会生成任何错误。但是当用户保存文档时,文档弹出“文档“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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
好的,按照 TechZen 的建议,捕获保存操作中的错误。将以下内容添加到 MyDocument.m
注意,如果有 100 个错误,那么您将收到包含 100 个项目的警报,其中不是最好的,但这是处理保存错误的一个很好的起点。
Ok, as TechZen suggested, capture the error from the save operation. Add the following to MyDocument.m
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.
验证错误表明问题在于保存文档时应用的验证谓词。反过来,这意味着您尝试保存的某些数据的类型或值错误。
如果您捕获保存操作返回的错误,则 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.
通常的嫌疑是一个(或两个)属性未设置为“OPTIONAL”,但没有任何值。
因此,为
awakeFromInsert
提供一个类别,该类别在NSManagedObject的
生命周期中仅调用一次。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 inNSManagedObject's
lifetime.如果您在核心数据属性中设置任何正则表达式验证,也会出现此类错误。也许你可以检查一下这个区域。我通过这种方式得到了这个错误。
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.
原因之一可能是,如果您尝试保存属性为空值的实体,而该属性配置为非可选。一种简单的检查方法是在 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