繁体   English   中英

iOS / Xcode:主线程检查器似乎没有激活,但由于在后台线程上调用UI而导致崩溃?

[英]iOS/Xcode: Main Thread Checker doesn't seem to be activating, but still a crash due to UI being called on background thread?

使用iOS应用程序时发生以下崩溃:

由于未捕获的异常'NSRangeException'而终止应用程序,原因:>'initWithCoder :: MKMapView>必须在主线程上初始化。

更为奇怪的是,调用堆栈表明这是源于我的代码(保存地图的控制器)中的嵌入序列。 该地图已经可见并且具有交互性,因此发生这种情况似乎很奇怪。

15 UIKit 0x00000001071ffc07 __67- [UIStoryboardEmbedSegueTemplate newDefaultPerformHandlerForSegue:] _ block_invoke + 180

我已经启用了主线程检查器, 在调试方案中暂停了问题(验证它是否设置为调试状态,并通过有意放入一些后台线程UI更新代码并在启动主线程检查器后将其删除来验证其运行)。

我本来以为Main Thread Checker会捕获我在做错了的re:map,无论如何,我都会得到一个方便的“在后台线程上调用的UI API”。 但是,除了崩溃外,我什么也没有得到,所以不知道我会在错误的re:线程上做些什么(我相信我的所有UI更新都包装在常规的dispatchqueue.main.async等中),以便可以对其进行修复。

a)我是否误解了主线程检查器的工作原理?

b)以前有人使用Mapkit + embed segues遇到过此问题吗?

c)还有另一种方法可以了解我可能做错了什么吗?

如果有帮助,则完整堆栈跟踪。 请注意,在标签和地图已经加载并且出现在用户界面中之后 ,我会收到此消息。

2017-12-20 23:04:34.829770-0800 XXXXXX[93019:3557775] This application is modifying the autolayout engine from a background thread after the engine was accessed from the main thread. This can lead to engine corruption and weird crashes.
 Stack:(
    0   Foundation                          0x0000000103a76ec6 _AssertAutolayoutOnAllowedThreadsOnly + 77
    1   Foundation                          0x0000000103880d7a -[NSISEngine withBehaviors:performModifications:] + 28
    2   UIKit                               0x0000000106ecb5e8 -[UIView(UIConstraintBasedLayout) _resetLayoutEngineHostConstraints] + 75
    3   UIKit                               0x00000001064356c2 -[UIView(CALayerDelegate) layoutSublayersOfLayer:] + 1388
    4   QuartzCore                          0x00000001061a13ee -[CALayer layoutSublayers] + 153
    5   QuartzCore                          0x00000001061a54dd _ZN2CA5Layer16layout_if_neededEPNS_11TransactionE + 401
    6   QuartzCore                          0x000000010612dded _ZN2CA7Context18commit_transactionEPNS_11TransactionE + 365
    7   QuartzCore                          0x0000000106159704 _ZN2CA11Transaction6commitEv + 500
    8   QuartzCore                          0x0000000106159d79 _ZN2CA11Transaction14release_threadEPv + 213
    9   libsystem_pthread.dylib             0x000000010b3d539f _pthread_tsd_cleanup + 544
    10  libsystem_pthread.dylib             0x000000010b3d50d9 _pthread_exit + 152
    11  libsystem_pthread.dylib             0x000000010b3d365d pthread_attr_getschedpolicy + 0
    12  libsystem_pthread.dylib             0x000000010b3d307d start_wqthread + 13
)
2017-12-20 23:04:34.850538-0800 XXXXXX[93019:3557775] *** Terminating app due to uncaught exception 'NSRangeException', reason: '<MKMapView: 0x7f8d30a9b200; frame = (0 0; 0 0); transform = [0, 0, 0, 0, 0, 0]; alpha = 0; opaque = NO; layer = (null)> initWithCoder:: MKMapView must be initialized on the main thread.'
*** First throw call stack:
(
    0   CoreFoundation                      0x0000000104bf71ab __exceptionPreprocess + 171
    1   libobjc.A.dylib                     0x0000000103e51f41 objc_exception_throw + 48
    2   CoreFoundation                      0x0000000104c6bcb5 +[NSException raise:format:] + 197
    3   MapKit                              0x0000000102e09546 -[MKMapView initWithCoder:] + 1368
    4   UIKit                               0x0000000106a42a45 UINibDecoderDecodeObjectForValue + 704
    5   UIKit                               0x0000000106a4277a -[UINibDecoder decodeObjectForKey:] + 89
    6   UIKit                               0x00000001067f0218 -[UIRuntimeConnection initWithCoder:] + 178
    7   UIKit                               0x0000000106a42a45 UINibDecoderDecodeObjectForValue + 704
    8   UIKit                               0x0000000106a42be3 UINibDecoderDecodeObjectForValue + 1118
    9   UIKit                               0x0000000106a4277a -[UINibDecoder decodeObjectForKey:] + 89
    10  UIKit                               0x00000001067ef415 -[UINib instantiateWithOwner:options:] + 1262
    11  UIKit                               0x0000000106508838 -[UIViewController _loadViewFromNibNamed:bundle:] + 383
    12  UIKit                               0x0000000106509164 -[UIViewController loadView] + 177
    13  UIKit                               0x0000000106509495 -[UIViewController loadViewIfRequired] + 195
    14  UIKit                               0x0000000106509cf2 -[UIViewController view] + 27
    15  UIKit                               0x0000000106fa0c07 __67-[UIStoryboardEmbedSegueTemplate newDefaultPerformHandlerForSegue:]_block_invoke + 180
    16  UIKit                               0x0000000106c8bdcb -[UIStoryboardSegueTemplate _performWithDestinationViewController:sender:] + 279
    17  UIKit                               0x0000000106c8bc83 -[UIStoryboardSegueTemplate _perform:] + 82
    18  UIKit                               0x0000000106c8bf4b -[UIStoryboardSegueTemplate perform:] + 157
    19  UIKit                               0x0000000106509685 -[UIViewController loadViewIfRequired] + 691
    20  UIKit                               0x0000000106509cf2 -[UIViewController view] + 27
    21  UIKit                               0x0000000106578094 -[UITabBarController transitionFromViewController:toViewController:transition:shouldSetSelected:] + 466
    22  UIKit                               0x0000000106577417 -[UITabBarController transitionFromViewController:toViewController:] + 59
    23  UIKit                               0x0000000106573404 -[UITabBarController _setSelectedViewController:] + 410
    24  UIKit                               0x00000001065731d3 -[UITabBarController setSelectedViewController:] + 109
    25  UIKit                               0x00000001064268f0 +[UIView(Animation) performWithoutAnimation:] + 90
    26  UIKit                               0x000000010656d6dc -[UITabBarController _selectDefaultViewControllerIfNecessaryWithAppearanceTransitions:] + 512
    27  UIKit                               0x000000010656e727 -[UITabBarController viewWillAppear:] + 211
    28  UIKit                               0x000000010650f59c -[UIViewController _setViewAppearState:isAnimating:] + 444
    29  UIKit                               0x000000010650fe31 -[UIViewController __viewWillAppear:] + 147
    30  UIKit                               0x00000001065517da -[UINavigationController _startTransition:fromViewController:toViewController:] + 843
    31  UIKit                               0x000000010655263f -[UINavigationController _startDeferredTransitionIfNeeded:] + 841
    32  UIKit                               0x00000001065538c3 -[UINavigationController __viewWillLayoutSubviews] + 150
    33  UIKit                               0x00000001067aca49 -[UILayoutContainerView layoutSubviews] + 231
    34  UIKit                               0x00000001064356f5 -[UIView(CALayerDelegate) layoutSublayersOfLayer:] + 1439
    35  QuartzCore                          0x00000001061a13ee -[CALayer layoutSublayers] + 153
    36  QuartzCore                          0x00000001061a54dd _ZN2CA5Layer16layout_if_neededEPNS_11TransactionE + 401
    37  QuartzCore                          0x000000010612dded _ZN2CA7Context18commit_transactionEPNS_11TransactionE + 365
    38  QuartzCore                          0x0000000106159704 _ZN2CA11Transaction6commitEv + 500
    39  QuartzCore                          0x0000000106159d79 _ZN2CA11Transaction14release_threadEPv + 213
    40  libsystem_pthread.dylib             0x000000010b3d539f _pthread_tsd_cleanup + 544
    41  libsystem_pthread.dylib             0x000000010b3d50d9 _pthread_exit + 152
    42  libsystem_pthread.dylib             0x000000010b3d365d pthread_attr_getschedpolicy + 0
    43  libsystem_pthread.dylib             0x000000010b3d307d start_wqthread + 13
)
libc++abi.dylib: terminating with uncaught exception of type NSException

根据文档

Main Thread Checker是用于Swift和C语言的独立工具,它可以检测AppKit,UIKit和其他API在后台线程上的无效使用。

他们对“其他API”的定义可能不包括MapKit。 我知道MKMapView从技术上是从NSView或UIView继承的,具体取决于平台,但是我想说主线程检查器可能不会在UIKit,AppKit和您的应用程序之外捕获任何警告或信号(我不知道当然,这只是一个猜测)。

一个常见的问题是尝试在viewDidLoad()执行segue,但似乎并没有这样做。

您是否正在执行导致无意中触发后台线程segue的操作?

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM