我通过扩展NSURLProtocol在我的应用程序中使用自定义URL协议.它大部分时间都可以正常工作但我看到通过crashlytics报告了以下崩溃.我自己无法重现这一点.最让我担心的是,我没有在崩溃的线程的堆栈跟踪中看到我的应用程序,所以我对从哪里开始调试毫无头绪.以下是崩溃报告
Thread : Crashed: com.apple.NSURLConnectionLoader 0 libsystem_kernel.dylib 0x3562cc84 __pthread_kill + 8 1 libsystem_pthread.dylib 0x356d0733 pthread_kill + 62 2 libsystem_c.dylib 0x355c4f21 abort + 108 3 libsystem_c.dylib 0x355a47eb __assert_rtn + 302 4 CFNetwork 0x22b82e45 CFURLProtocol_NS::_protocolInterface_cancelLoad() + 322 5 CFNetwork 0x22c3740f ___ZN19URLConnectionLoader27_private_ScheduleOriginLoadEPK12NSURLRequestPK20_CFCachedURLResponse_block_invoke_2 + 38 6 CFNetwork 0x22b66ccd ___ZNK19URLConnectionLoader25withExistingProtocolAsyncEU13block_pointerFvP11URLProtocolE_block_invoke + 16 7 libdispatch.dylib 0x35513bd7 _dispatch_client_callout + 22 8 libdispatch.dylib 0x3551d187 _dispatch_block_invoke$VARIANT$mp + 446 9 CFNetwork 0x22b66caf RunloopBlockContext::_invoke_block(void const*,void*) + 18 10 CoreFoundation 0x2326ab51 CFArrayApplyFunction + 36 11 CFNetwork 0x22b66b97 RunloopBlockContext::perform() + 182 12 CFNetwork 0x22b66a61 MultiplexerSource::perform() + 216 13 CFNetwork 0x22b668f9 MultiplexerSource::_perform(void*) + 48 14 CoreFoundation 0x23319bff __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION__ + 14 15 CoreFoundation 0x233197ed __CFRunLoopDoSources0 + 452 16 CoreFoundation 0x23317b5b __CFRunLoopRun + 794 17 CoreFoundation 0x2326b119 CFRunLoopRunSpecific + 520 18 CoreFoundation 0x2326af05 CFRunLoopRunInMode + 108 19 CFNetwork 0x22bd8bbf +[NSURLConnection(Loader) _resourceLoadLoop:] + 486 20 Foundation 0x241291b5 __NSThread__start__ + 1148 21 libsystem_pthread.dylib 0x356cf85b _pthread_body + 138 22 libsystem_pthread.dylib 0x356cf7cf _pthread_start + 110 23 libsystem_pthread.dylib 0x356cd724 thread_start + 8
堆栈跟踪中有趣的部分是这几行.
4 CFNetwork 0x22b82e45 CFURLProtocol_NS::_protocolInterface_cancelLoad() + 322 5 CFNetwork 0x22c3740f ___ZN19URLConnectionLoader27_private_ScheduleOriginLoadEPK12NSURLRequestPK20_CFCachedURLResponse_block_invoke_2 + 38 6 CFNetwork 0x22b66ccd ___ZNK19URLConnectionLoader25withExistingProtocolAsyncEU13block_pointerFvP11URLProtocolE_block_invoke + 16
我可以通过查看NSURLProtocol中startLoading和stopLoading方法的堆栈跟踪来确定___ZN19URLConnectionLoader27_private_ScheduleOriginLoadEPK12NSURLRequestPK20_CFCachedURLResponse_block_invoke_2调用startLoading,而CFURLProtocol_NS :: _ protocolInterface_cancelLoad()由于取消请求而调用stopLoading.那么,为什么或如何在尝试开始加载后取消调用?
任何帮助是极大的赞赏.谢谢.
更新:
我能够重现一个类似的(不精确的跟踪),我看到以下断言..
Assertion Failed: (_protocolInstance == nil),function _protocolInterface_startLoad,file /buildroot/Library/Caches/com.apple.xbs/Sources/CFNetwork/CFNetwork-758.0.2/Session/LocalSession.mm,line 1341.
以下是后面的跟踪.
(lldb) bt * thread #6: tid = 0xe687,0x34515d24 libsystem_kernel.dylib`__pthread_kill + 8,name = 'com.apple.NSURLConnectionLoader',stop reason = signal SIGABRT * frame #0: 0x34515d24 libsystem_kernel.dylib`__pthread_kill + 8 frame #1: 0x345b974a libsystem_pthread.dylib`pthread_kill + 62 frame #2: 0x344adf40 libsystem_c.dylib`abort + 108 frame #3: 0x3448d80a libsystem_c.dylib`__assert_rtn + 302 frame #4: 0x2202de4c CFNetwork`CFURLProtocol_NS::_protocolInterface_startLoad(_CFCachedURLResponse const*) + 324 frame #5: 0x220e22e6 CFNetwork`___ZN19URLConnectionLoader27_private_ScheduleOriginLoadEPK12NSURLRequestPK20_CFCachedURLResponse_block_invoke_2 + 38 frame #6: 0x22011cd4 CFNetwork`___ZNK19URLConnectionLoader25withExistingProtocolAsyncEU13block_pointerFvP11URLProtocolE_block_invoke + 16 frame #7: 0x003a5d72 libdispatch.dylib`_dispatch_client_callout + 22 frame #8: 0x003ad8d8 libdispatch.dylib`_dispatch_block_invoke + 468 frame #9: 0x22011cb6 CFNetwork`RunloopBlockContext::_invoke_block(void const*,void*) + 18 frame #10: 0x22710c80 CoreFoundation`CFArrayApplyFunction + 36 frame #11: 0x22011b9e CFNetwork`RunloopBlockContext::perform() + 182 frame #12: 0x22011a68 CFNetwork`MultiplexerSource::perform() + 216 frame #13: 0x22011900 CFNetwork`MultiplexerSource::_perform(void*) + 48 frame #14: 0x227bfc3e CoreFoundation`__CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION__ + 14 frame #15: 0x227bf7c0 CoreFoundation`__CFRunLoopDoSources0 + 344 frame #16: 0x227bdb9a CoreFoundation`__CFRunLoopRun + 794 frame #17: 0x22711248 CoreFoundation`CFRunLoopRunSpecific + 520 frame #18: 0x22711034 CoreFoundation`CFRunLoopRunInMode + 108 frame #19: 0x22083ee6 CFNetwork`+[NSURLConnection(Loader) _resourceLoadLoop:] + 486 frame #20: 0x235cc634 Foundation`__NSThread__start__ + 1148 frame #21: 0x345b8872 libsystem_pthread.dylib`_pthread_body + 138 frame #22: 0x345b87e6 libsystem_pthread.dylib`_pthread_start + 110 frame #23: 0x345b6740 libsystem_pthread.dylib`thread_start + 8
这是我的协议类的简化版本,其中MyConnection的工作方式与NSURLConnection非常相似.
@implementation MyProtocol
- (void) startLoading { NSURLRequest *request = self.request; self.myConnection = [[MyConnection alloc] initWithRequest:request delegate:self startImmediately:NO]; NSRunLoop *loop = [NSRunLoop currentRunLoop]; [self.myConnection scheduleInRunLoop:loop forMode:loop.currentMode]; [self.myConnection start]; } - (void) stopLoading { if (self.myConnection) { [self.myConnection cancel]; self.myConnection = nil; } } #pragma mark MyConnectionDelegate - (void) myConnection:(MyConnection *)connection didReceiveData:(NSData *)data { [self.client URLProtocol:self didLoadData:data]; } - (void) myConnectionDidFinishLoading:(MyConnection *)connection { [self.client URLProtocolDidFinishLoading:self]; } //..other delegate methods are implemented similarly
之间,我看到的异常代码是
Exception Type: EXC_CRASH (SIGABRT) Exception Codes: 0x0000000000000000,0x0000000000000000 Exception Note: EXC_CORPSE_NOTIFY Triggered by Thread: 4
解决方法
我不知道你是否已经这样做但是可能试图强制启动和取消在同一个线程上?
尝试使用-com.apple.CoreData.ConcurrencyDebug 1用于调试 – 我不确定它是否仅调试CoreData线程但值得尝试.