Core Data: Errors vs Exceptions Part 2

China☆狼群 提交于 2019-12-06 11:05:49

问题


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.


回答1:


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.



来源:https://stackoverflow.com/questions/2024246/core-data-errors-vs-exceptions-part-2

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!