奇怪的 iPhone CORE DATA 错误行为

发布于 2024-08-21 21:49:19 字数 538 浏览 2 评论 0原文

我在核心数据应用程序中有一个非常奇怪的行为。 这是代码:

        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 技术交流群。

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

发布评论

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

评论(3

不离久伴 2024-08-28 21:49:19

您需要设置 NSError *error = nil; 以确保安全,否则您将分配到未确定的内存位置。

就像 gerry3 提到的那样,您可能有一个 nil ManagedObjectContext。我建议将代码更改为:

unaReg.valore = [NSNumber numberWithInt:val];
NSError *error = nil;
NSAssert(managedObjectContext != nil, @"Context is nil");
if (![managedObjectContext save:&error]) {
    [myUtil manageError:[error localizedDescription]];
}

这是 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:

unaReg.valore = [NSNumber numberWithInt:val];
NSError *error = nil;
NSAssert(managedObjectContext != nil, @"Context is nil");
if (![managedObjectContext save:&error]) {
    [myUtil manageError:[error localizedDescription]];
}

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.

傲性难收 2024-08-28 21:49:19

仔细检查您的托管对象上下文是否已设置:

    unaReg.valore = [NSNumber numberWithInt:val];
    NSError *error;
    NSLog(@"moc = %@",managedObjectContext);
    if (![managedObjectContext save:&error]) {
        [myUtil manageError:[error localizedDescription]];
    }

Double check that your managed object context is set:

    unaReg.valore = [NSNumber numberWithInt:val];
    NSError *error;
    NSLog(@"moc = %@",managedObjectContext);
    if (![managedObjectContext save:&error]) {
        [myUtil manageError:[error localizedDescription]];
    }
转角预定愛 2024-08-28 21:49:19

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

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