问题
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