【发布时间】:2017-11-24 13:14:49
【问题描述】:
在处理 Apple 的按需资源框架时,我的应用发现为一部分用户生成了许多异常。我解决此问题的第一次尝试是将我的代码包装在 @try/@catch 块中,但不幸的是,崩溃仍然传播。我已经缩小了问题的范围,但不确定如何解决。似乎 Apple 使用 NSBundleResourceRequest 的代码使用了一个 dispatch_once 块,其中会生成一个 Obj-C 异常。我编写了以下测试代码来查看使用 dispatch_once 块处理异常的行为:
static dispatch_once_t onceToken;
@try {
// 1.) Expected: Caught by first catch block
// @throw [NSException exceptionWithName:NSInternalInconsistencyException reason:@"Couldn't find" userInfo:nil];
// 2.) Expected: Caught by second catch block
// @throw NSInternalInconsistencyException;
// 3.) Will not be caught be the surrounding @try/@catch block
dispatch_once(&onceToken, ^{
@throw [NSException exceptionWithName:NSInternalInconsistencyException reason:@"Couldn't find" userInfo:nil];
});
} @catch(NSException* e) {
DDLogError(@"Catch: NSException");
} @catch(id e) {
DDLogError(@"Catch: Obj-C");
} @catch (...) {
DDLogError(@"Catch: ...");
}
从我的 cmets 中可以看出,在 dispatch_once 块中生成的异常不会被周围的 @try/@catch 捕获。
我的问题是:
如何捕获在 dispatch_once 块中生成的异常,我无法访问其源代码(Apple 的)?
关于这个问题,除了不使用 Apple 的 ODR 框架之外,我还能做些什么来让我的应用更加稳定?
附加信息(如果有帮助):
我要解决的具体堆栈跟踪是以下代码。这不是 NSBundleResourceRequest 的唯一问题,所有其他问题都遵循相同的范例。
Application Specific Information:
*** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'NSBundleResourceRequest could not connect to its helper daemon'
Last Exception Backtrace:
0 CoreFoundation 0x00000001857f3d38 __exceptionPreprocess + 124
1 libobjc.A.dylib 0x0000000184d08528 objc_exception_throw + 52
2 Foundation 0x000000018623e9d0 _setupConnection + 1688
3 libdispatch.dylib 0x0000000185179048 _dispatch_client_callout + 12
4 libdispatch.dylib 0x000000018517c710 dispatch_once_f$VARIANT$mp + 56
5 Foundation 0x000000018623e2e8 +[NSBundleResourceRequest _connection] + 64
6 Foundation 0x000000018623d1ac -[_NSBundleODRDataForApplications initWithBundle:] + 124
7 Foundation 0x000000018623d07c +[_NSBundleODRDataForApplications dataForBundle:createIfRequired:] + 180
8 Foundation 0x000000018623fba0 -[NSBundleResourceRequest conditionallyBeginAccessingResourcesWithCompletionHandler:] + 76
【问题讨论】:
-
嗨,你有答案了吗?
-
不,我没有。我不确定这是否可能。