NSManagedObjectContext:异常断点停止在save:方法,但没有日志/崩溃/错误 [英] NSManagedObjectContext: exception breakpoint stops at save: method, but no log/crash/error

查看:820
本文介绍了NSManagedObjectContext:异常断点停止在save:方法,但没有日志/崩溃/错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在一个多线程iOS应用程序中使用CoreData,并且一切似乎都很好 - 除非我打开XCode中的异常断点。每当我做一些CoreData工作时,断点停止在 save: - 方法在 NSManagedObjectContext - 但NSError是nil之后。我也没有在日志中(除​​了 Catchpoint 2(异常抛出)。),应用程序不会崩溃...所以很难说出什么错了。

I'm using CoreData in a multi-threaded iOS-app, and everything seems to work fine - unless I switch on the exception breakpoint in XCode. Whenever I do some CoreData-work, the breakpoint stops at the save:-method on NSManagedObjectContext - but the NSError is nil afterwards. I also have nothing in the log (except: Catchpoint 2 (exception thrown).), the app doesn't crash... So it's pretty hard to tell what's going wrong.

我唯一的线索是我在 updatedObjects中有一个对象: c> NSManagedObjectContext - 但似乎没有什么错误。

The only clue I have is that I have a single object in updatedObjects: in my NSManagedObjectContext - but there seems nothing wrong with it.

我的问题是非常类似于这个问题在stackoverflow
,但是唯一的答案没有帮助我;

My question is very similar to this question on stackoverflow , but the only answer there doesn't help me; I'm pretty sure that I've got everything covered there.

这里可能出错了什么?

What could be wrong here? Or are there other possibilities to get some error information?

非常感谢!

编辑:显示代码相当困难。我加载对象与objectID,编辑并将其存储在分配给当前线程的上下文中。我已经检查 - 上下文总是正确的当前线程;每个线程都有自己的上下文,这不应该是问题。如果只有某人能告诉我如何从错误/异常获取更多信息,或者如果我必须关心它,这将是有用的。在我看来似乎在`save'-method中捕获了异常,所以它可能是一个正常行为?

EDIT: showing code is pretty difficult. I'm loading objects with objectID, edit and store them in the context assigned to the current thread. I already checked - the context is always correct for the current thread; each thread has its own context, that shouldn't be the problem. It would be even helpful if only someone could tell me how to get more information from that error/exception - or if I have to care about it after all. It seems to me as if the exception is catched within the `save´-method, so probably its a "normal" behaviour?

推荐答案

p>这是正常行为。 CoreData使用异常抛出&处理其内部的一些程序流。我跟CoreData人谈了这个。这可能看起来很奇怪,但这是他们很久以前做出的设计决定。

This is normal behavior. CoreData uses exception throwing & handling internally for some of its program flow. I talked to the CoreData people about this. It may seem odd, but that's a design decision they made a long time ago.

当你遇到异常时,确保你的代码没有在你的调用 - [NSManagedObjectContext save:] 并抛出异常。调用 -save:很有可能回调您的代码,例如。如果你观察 NSManagedObjectContextObjectsDidChangeNotification ,如果你在处理这些通知时做坏事,显然你是错误的。

When you hit the exception, make sure there's none of your code in the backtrace between your call to -[NSManagedObjectContext save:] and the exception being thrown. Calling -save: is very likely to call back into your code, e.g. if you're observing NSManagedObjectContextObjectsDidChangeNotification, and if you're doing bad things when you're handling those notification, obviously you're at fault.

如果退出 -save:方法,返回值为 YES ,一切都很好。

If you're exiting the -save: method, and the return value is YES, everything is good.

请注意,您应该检查返回值,不要使用错误! = nil 以检查错误。正确的检查是:

Please note, that you should check the return value, do not use error != nil to check for an error. The correct check is:

NSError *error = nil;
BOOL success = [moc save:&error];
if (!success) {
    // do error handling here.
}

这篇关于NSManagedObjectContext:异常断点停止在save:方法,但没有日志/崩溃/错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆