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

问题描述 投票:0回答:1

使用以下崩溃在iOS应用上工作:

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

更奇怪的是,调用堆栈表明这源于我的代码(保存地图的控制器)中的嵌入segue。然而,地图已经可见并且是交互式的,所以看起来这很奇怪。

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

我已启用主线程检查器并暂停我的调试方案中的问题(验证它已设置为调试,并通过故意放入一些后台线程UI更新代码并在主线程检查器启动后将其删除来验证它的操作)。

我会假设主线程检查器会抓住任何它我正在做的错误:地图,我会得到一个方便的“UI API在后台线程上调用”。但是,我得到的只是崩溃,所以不知道我会做错了什么:线程(我相信所有的UI更新都包含在通常的dispatchqueue.main.async等中),以便我可以解决它。

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

b)之前有没有人使用Mapkit +嵌入segues来解决这个问题?

c)是否有另一种方法来捕捉它可能做错了什么?

万一它有帮助,完整的堆栈跟踪。请注意,我在选项卡和地图已经加载并且存在于UI中后收到此信息。

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
ios xcode multithreading mapkit
1个回答
0
投票

the docs说:

主线程检查器是Swift和C语言的独立工具,可检测后台线程上AppKit,UIKit和其他API的无效使用。

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

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

您是否正在做任何导致无意中触发后台线程上的segue的事情?

© www.soinside.com 2019 - 2024. All rights reserved.