0

进一步解决我的问题和这里提到的解决方案:https ://stackoverflow.com/a/19153387/260665解决问题的唯一方法是@try-@catch通过清除线程的结果来实现块并优雅地处理异常以防万一的例外。

这是我的 try-catch 块:

    // These operations are meant to calculate statistics for the Task objects, now when the statistics are being calculated there are possibilities that the main thread might delete the ManagedObject from it's MOC and write to the store. Now this means that ManagedObjects here may not have an equivalent record back in the store to refer!
    // So, when the already faulted objects try to access it's properties, NSObjectInaccessibleException arises. For more details check the SO post: https://stackoverflow.com/questions/18828164/coredata-object-fault-in-independent-managedobjectcontext-vs-changes-in-persiste
    // Hence catching the exception and returning back no results is the best approach as of now!
    @try
    {
        CSStatistics *theStats = [self statisticsOperationFromDate:self.dateRange.fromDate
                                                            toDate:self.dateRange.toDate];
        [self setOutStatistics:theStats];

        if ([(NSObject*)[self resultDelegate] respondsToSelector:@selector(statisticsOperationCompletedWithOperation:)])
        {
            [(NSObject*)[self resultDelegate] performSelectorOnMainThread:@selector(statisticsOperationCompletedWithOperation:)
                                                               withObject:self
                                                            waitUntilDone:NO];
        }
    }
    @catch (NSException *exception)
    {
        DEBUGLog(@"Most probably a NSObjectInaccessibleException exception! - %@", exception);
        [self setOutStatistics:nil];    // We would not like to give out erroneous reports! Mind it!
    }
    @finally
    {

    }

奇怪的事情发生了,我的 try-catch 块在分发构建中运行良好,因为应用程序在调试和发布构建中崩溃。显然,引发的异常是在某处处理的,它不会传递给@catch块,而是被消耗掉。我无法弄清楚 Xcode 在哪里没有提供更多相同的细节。

这是调试模式下的日志:

2013-10-23 14:33:07.293 CATSXT[64267:650b] *** Terminating app due to uncaught exception 'NSObjectInaccessibleException', reason: 'CoreData could not fulfill a fault for '0x1912fc00 <x-coredata://6E14A109-8A14-4369-BBC5-468D790A8D9E/CSTask/p7562>''
*** First throw call stack:
(0x33af012 0x2d25e7e 0x2a4ba48 0x2a4b3f7 0x2a4b031 0x2a4aea6 0x10eb42e2 0x5e02d 0x242e00b 0x2a86fc4 0x5cc58 0x244c247 0x5d492 0x244c205 0x5d492 0x246d23c 0x2440c70 0x15c075 0x15caac 0x15c6dd 0x244e453 0x244e164 0x24daa31 0x601253f 0x6024014 0x60152e8 0x6015450 0x949b0e72 0x94998d2a)
libc++abi.dylib: terminate called throwing an exception

一些谷歌搜索向我显示“所有异常”断点会导致这种行为,但我也禁用了该断点。现在我仍然无法弄清楚为什么@catch在调试和发布版本中不执行块,但它在分发版本中运行良好。这不会给生产应用程序的用户带来麻烦,但肯定会损害开发过程。

任何线索将不胜感激。

4

0 回答 0