xcode NSManagedObjectContext 保存不会崩溃,但会在 objc_exception_throw 上中断
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7007660/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
NSManagedObjectContext save doesn't crash but breaks on objc_exception_throw
提问by AmineG
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)
我正在调试一个使用 Core Data 和多线程的应用程序,我在 objc_exception_throw 上有一个断点,它在调用 save 时遇到了这个断点。(代码中的第 2 行)
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.
我没有任何记录的东西。我在 ios 4.0 -> 4.3 上使用 Xcode 4。我认为这与 Xcode/iOS 版本无关。
采纳答案by Leonardo
- 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.
- 首先,当在 CoreData 中使用多线程时,在应用程序周围传递 NSManagedObject 时几乎没有问题。相反,正如 Apple 所记录的那样,我最终传递了 NSManagedObjectID 并重建了完整的对象。
- 其次,当您没有记录任何内容时,这可能与内存问题有关,尤其要尝试运行分析器,但不仅要查找“Zombie”,它还会告诉您更多信息。
- 最后,确保您已正确初始化上下文,我遇到了类似的问题,因为未找到且未加载来自 momd 文件的模型。
回答by Patrik
Looking at this answerreveals 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.
查看此答案表明 CoreData 在内部使用异常来管理其程序流。这就是调试器在 objc_exception_throw 处中断的原因。据我所知,没有办法禁用它。
EDIT: Since then, there is now a solution to ignore these exceptions: Ignore certain exceptions when using Xcode's All Exceptions breakpoint
编辑:从那时起,现在有一个解决方案可以忽略这些异常:使用 Xcode 的所有异常断点时忽略某些异常
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:
顺便说一句:不要检查error
而是使用返回的BOOL
值来确保您的保存调用成功。这样做的正确方法是:
NSError *error = nil;
BOOL success = [self.managedObjectContext save:&error];
if (!success) {
NSLog(@"Error : %@",error);
}
回答by Yonat
I had a similar problem, eventually it turned out to be because of an observer of NSManagedObjectContextDidSaveNotificationthat 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.
我有一个类似的问题,最终结果是因为NSManagedObjectContextDidSaveNotification的观察者在没有从通知中心移除自己的情况下被释放。似乎 CoreData 异常“隐藏”了未知选择器异常,当通知中心尝试通知任何对象占用已注册(但已解除分配)的观察者释放的内存时,会引发该异常。
回答by wintvelt
Recently I ran into the same issue: app crashing without any log, when trying to save managedObjectContext.
最近我遇到了同样的问题:尝试保存 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.
就我而言,有一个与上述完全不同的原因:
确保您的 mac 上没有打开 DB Manager,这可能会锁定持久数据存储。
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.
原来我有一些未保存的更改,这使数据库管理器锁定了存储。关闭数据库管理器解决了这个问题。简单而愚蠢的错误,但我花了几个小时才弄明白。
回答by shicong zhao
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* 错误,请问您在外部范围内是否还有另一个 NSError* 错误,例如:
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.
然后错误将为零,尽管实际上有一个例外。