奇怪的 iPhone CORE DATA 错误行为
我在核心数据应用程序中有一个非常奇怪的行为。 这是代码:
unaReg.valore = [NSNumber numberWithInt:val];
NSError *error;
if (![managedObjectContext save:&error]) {
[myUtil manageError:[error localizedDescription]];
}
unaReg
是一个核心数据实体,我想更新其 valore
属性。 当我运行 save
命令 (managementObjectContext save:&error
) 时,我收到错误,因为程序在 if
分支内流动。奇怪的行为是 error
变量为 nil
并且新值正确保存在数据库中(因此看起来没有错误)。 怎么了???? 谢谢。
I have a really strange behavior in a Core Data app.
This is the code:
unaReg.valore = [NSNumber numberWithInt:val];
NSError *error;
if (![managedObjectContext save:&error]) {
[myUtil manageError:[error localizedDescription]];
}
unaReg
is a Core Data Entity and I want to update its valore
property.
When I run the save
command (managedObjectContext save:&error
) I get an error since the program flows inside the if
branch. The strange behavior is that the error
variable is nil
and the new value is saved correctly inside the database (so it seems that there is not an error).
What's wrong????
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您需要设置 NSError *error = nil; 以确保安全,否则您将分配到未确定的内存位置。
就像 gerry3 提到的那样,您可能有一个 nil ManagedObjectContext。我建议将代码更改为:
这是 NSAssert 语句的完美用途,因为您可以在开发时使用它们进行内联测试,并通过一个开关将它们全部关闭以进行生产。
如果你的 ManagedObjectContext 是 nil ,那么你会从 -save: 得到一个错误的响应,并且因为你没有将 error 设置为 nil ,它将指向内存中的“某物”,进一步引起混乱。
You need to set
NSError *error = nil;
to be safe otherwise you are going to get an undetermined memory location assigned.Like gerry3 mentioned, you probably have a nil managedObjectContext. I would recommend changing the code to:
This is a perfect use for NSAssert statements, because you can use them to test inline while developing and with one switch, turn them all off for production.
If your managedObjectContext is nil then you will get a false response from -save: and because you did not set error to nil it will be pointing to "something" in memory, further causing confusion.
仔细检查您的托管对象上下文是否已设置:
Double check that your managed object context is set:
如果 ManagedObjectContext 不为 null,那么您可能还需要检查存储在 ManagedObjectContext 中的其他托管对象。有时,当您只需要保存
unaReg
对象时,错误可能会出现在删除其他一些对象时。我通常做的事情是:
- 检查
删除规则
上的数据模型,确保您已正确配置所有数据模型- 在执行 ManagedObjectContext 的保存操作时,确保其他对象因
删除规则
而以某种方式被删除。还有一些我遇到过但现在想不起来的情况。
希望有帮助
If the managedObjectContext is not null, then you may need to check other managed objects that you stored in your managedObjectContext as well. Sometimes, the error might be located in the deletion of some other objects, when you just need to save your
unaReg
object.What I usually do are:
- Check your data models on the
Delete rule
, make sure that you have correctly configured all of them- On your save action of managedObjectContext, ensure that other objects are in some way deleted due to the
Delete rule
.Some other situation that I have encountered but right now I can not think of.
Hope it helps