核心数据:错误与异常第2部分 [英] Core Data: Errors vs Exceptions Part 2
问题描述
我的问题类似于这一个,但我需要进一步澄清。我经常在这样的代码中得到例外:
NSError *错误;
if(![managedObjectContext save:& error]){
NSLog(@Failed to save to data store:%@,[error localizedDescription]);
}
然而,当我在 objc_exception_throw
,我可以发现在 save
中抛出异常:
$ g code code code code $ c >
我不会指望这个,因为 docs 表示乐观的锁定失败将返回错误,而不是例外。
除此之外,我甚至不能在代码中使用
@try ... @catch(NSException * e)
来捕获此异常。这很奇怪。我认为发生的是 objc_exception_throw
捕获Core Data中的内部异常,但是它们真的不会渗透到我的应用程序中。因为我设置了我的合并策略,所以锁定失败被转换成对象合并,一切都很好。
我也应该使用 committedValuesForKeys: / code>所以我可以看到例外之前发生了什么..这不会每次发生。
My question is similar to this one, but I need further clarification. I often get exceptions during code like this:
NSError* error;
if (![managedObjectContext save:&error]) {
NSLog(@"Failed to save to data store: %@", [error localizedDescription]);
}
However, when I put a breakpoint in objc_exception_throw
, I can find out that an exception is thrown in save
:
(gdb) po [$eax name]
NSInternalInconsistencyException
(gdb) po [$eax description]
optimistic locking failure
I wouldn't expect this, since the docs say that an optimistic locking failure will return an error, not an exception.
As an aside, I can't even seem to catch this exception with @try ... @catch (NSException * e)
in the code. It's all strange.
I think what's happening is that objc_exception_throw
is catching internal exceptions in Core Data, but they really aren't percolating to my app. Because I set my merge policy, the locking failures are getting converted into object merges and all is well.
I should probably also use committedValuesForKeys:
so I can see what's going on before the exceptions .. this doesn't happen every time.
这篇关于核心数据:错误与异常第2部分的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!