NSManagedObjectContext 保存不会崩溃,但会在 objc_exception_throw 上中断

发布于 2024-11-28 23:14:09 字数 673 浏览 1 评论 0原文

我遇到了此地址中描述的相同问题 http://www.cocoabuilder.com/archive/cocoa/288659-iphone-nsmanagementobjectcontext-save-doesn-crash-but-breaks-on-objc-exception-throw.html

我正在调试一个使用 Core Data 进行多线程处理的应用程序,并且我在 objc_exception_throw 上有一个断点,并且它在调用保存时命中了该断点。 (代码中的第 2 行)

        NSError *error = nil;
        [self.managedObjectContext save:&error];
        if (error) {
            NSLog(@"Error : %@",error);
        }

我没有记录任何内容。 我正在使用 Xcode 4 和 ios 4.0 -> 4.3.我认为这与 Xcode/iOS 版本无关。

I am having the same issue described at this address http://www.cocoabuilder.com/archive/cocoa/288659-iphone-nsmanagedobjectcontext-save-doesn-crash-but-breaks-on-objc-exception-throw.html

I am debugging an application that uses Core Data with multithreading, and I have a breakpoint on objc_exception_throw and it hits this breakpoint in the call to save. (line 2 in code)

        NSError *error = nil;
        [self.managedObjectContext save:&error];
        if (error) {
            NSLog(@"Error : %@",error);
        }

I don't have any thing that is logged.
I am using Xcode 4 with ios 4.0 -> 4.3. I think this is not related to Xcode/iOS version.

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

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

发布评论

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

评论(5

欢你一世 2024-12-05 23:14:09

查看这个答案表明CoreData内部使用异常来管理其程序流。这就是调试器在 objc_exception_throw 处中断的原因。据我所知,没有办法禁用它。

编辑:从那时起,现在有一个解决方案可以忽略这些异常: 使用 Xcode 的 All Exceptions 断点时忽略某些异常

BTW:不要检查 error 而是使用返回的 BOOL 值来确保保存调用成功。正确的做法是:

NSError *error = nil;
BOOL success = [self.managedObjectContext save:&error];
if (!success) {
    NSLog(@"Error : %@",error);
}

Looking at this answer reveals that CoreData internally uses exceptions to manage their program flow. Thats why the debugger breaks at objc_exception_throw. As far as I know there is no way to disable this.

EDIT: Since then, there is now a solution to ignore these exceptions: Ignore certain exceptions when using Xcode's All Exceptions breakpoint

BTW: Do not check on error but use the returned BOOL value to ensure success of your save call. The correct way of doing this would be:

NSError *error = nil;
BOOL success = [self.managedObjectContext save:&error];
if (!success) {
    NSLog(@"Error : %@",error);
}
听风吹 2024-12-05 23:14:09
  • 首先,当将多线程与 CoreData 一起使用时,我遇到了一些问题
    在应用程序中传递 NSManagedObject。相反,正如记录的
    Apple,我最终传递了 NSManagedObjectID 并重建了完整的
    目的。
  • 其次,当您没有记录任何内容时,它可能与
    内存问题,尝试特别运行探查器,但不仅仅是寻找“僵尸”,它
    应该告诉你更多。
  • 最后,确保您已正确初始化上下文,我有
    类似的问题,因为未找到 momd 文件中的模型并且
    未加载。
  • First, when using multithread with CoreData I had few problems when
    passing NSManagedObject around the app. Instead, as documented by
    Apple, I end up passing NSManagedObjectID and reconstruct the full
    object.
  • Second, when you don't have anything logged, it is likely related to
    memory issues, try to run the profiler especially, but not only, looking for 'Zombie', it
    should tell you more.
  • Finally, make sure you have initialized the context correctly, I had
    similiar problem because the model from momd file was not found and
    not loaded.
心头的小情儿 2024-12-05 23:14:09

我遇到了类似的问题,最终证明是由于 NSManagedObjectContextDidSaveNotification 的观察者被释放而没有将其从通知中心中删除。看来 CoreData 异常“隐藏”了当通知中心尝试通知任何对象占用由注册(但已释放)观察者释放的内存时引发的未知选择器异常。

I had a similar problem, eventually it turned out to be because of an observer of NSManagedObjectContextDidSaveNotification that was deallocated without removing itself from the notification center. It seems that the CoreData exception "hides" the unknown selector exception that is raised when the notification center tries notifying whatever object occupies the memory freed by the registered (but deallocated) observer.

你げ笑在眉眼 2024-12-05 23:14:09

最近我遇到了同样的问题:当尝试保存 ManagedObjectContext 时,应用程序崩溃,没有任何日志。

就我而言,有与上述完全不同的原因:
确保您没有在 Mac 上打开数据库管理器,这可能会锁定持久数据存储。

这(显然)也会杀死核心日期堆栈,而不会在代码或代码中引发任何可见错误日志。

结果我有一些未保存的更改,这使得数据库管理器锁定了存储。关闭数据库管理器解决了该问题。简单而愚蠢的错误,但我花了几个小时才弄清楚。

Recently I ran into the same issue: app crashing without any log, when trying to save managedObjectContext.

In my case there was a completely different cause from mentioned above:
Make sure you do not have DB Manager open on your mac, that could be locking the persistent data store.

This will (apparently) also kill the Core Date stack, without throwing any visible errors in code or in log.

Turned out I had some unsaved changes, which made the DB Manager lock the store. Closing the DB Manager fixed the issue. Simple and stupid error, but took me hours to figure out.

心碎的声音 2024-12-05 23:14:09

我碰巧遇到了这个问题,经过长时间的调试,我发现这是因为 NSError* 错误的重复声明,可能你在外部作用域中还有另一个 NSError* 错误,例如:

NSError* error = nil;

某些代码

if (!error)
{
    NSError* error = nil;
    // your code
}

然后错误将为零,尽管在事实上有一个例外。

I happened to meet this problem, and after long time debug I found it's because of a duplicate declaration of the NSError* error, may you had another NSError* error in the outer scope, like:

NSError* error = nil;

Some code

if (!error)
{
    NSError* error = nil;
    // your code
}

Then the error will be nil although in fact there is a exception.

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