如何解决iOS 14 上的 Objective-C UIAlertController 崩溃
我有一个用作 QRCode 扫描器的视图控制器,下面是关于扫描输出的部分。它在 iOS 12/13 上运行良好,但仅在 iOS 14 上崩溃。
AVMetadataMachineReadableCodeObject *metadataObj = [metadataObjects objectAtIndex:0];
if ([[metadataObj type] isEqualToString:AVMetadataObjectTypeQRCode]) {
NSString *codeString=[metadataObj stringValue];
[self performSelectorOnMainThread:@selector(stopReading) withObject:nil waitUntilDone:NO];
UIAlertController * alert = [UIAlertController
alertControllerWithTitle:@"QRCode is Scanned"
message:@""
preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction* ok = [UIAlertAction
actionWithTitle:@"OK"
style:UIAlertActionStyleDefault
handler:^(UIAlertAction * action) {
// [self startReading];
// [_viewPreview bringSubviewToFront:_previewFrame];
NSLog(@"The Scanned QRCode as %@",codeString);
[self.delegate addItemViewController:self didFinishEnteringItem:codeString];
[self.navigationController popViewControllerAnimated:YES];
}];
[alert addAction:ok];
[self presentViewController:alert animated:YES completion:nil];
然后崩溃日志如下所示,这在代码上显示为“UIAlertController * alert”,我认为iOS 14可能有问题。
2021-04-15 16:21:04.194 ntpc[5257:1741639] 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 CoreAutoLayout 0x00000001c1b80548 011F5CA5-D31B-3CDC-83C2-C7CB66064075 + 62792
1 CoreAutoLayout 0x00000001c1b80904 011F5CA5-D31B-3CDC-83C2-C7CB66064075 + 63748
2 UIKitCore 0x00000001b0204ef0 CC6E5AC7-8248-35F6-8B42-2E25C93DCF0A + 16047856
3 UIKitCore 0x00000001b0206294 CC6E5AC7-8248-35F6-8B42-2E25C93DCF0A + 16052884
4 UIKitCore 0x00000001af33d0a0 CC6E5AC7-8248-35F6-8B42-2E25C93DCF0A + 549024
5 UIKitCore 0x00000001af338880 CC6E5AC7-8248-35F6-8B42-2E25C93DCF0A + 530560
6 UIKitCore 0x00000001af338aec CC6E5AC7-8248-35F6-8B42-2E25C93DCF0A + 531180
7 UIKitCore 0x00000001af338f7c CC6E5AC7-8248-35F6-8B42-2E25C93DCF0A + 532348
8 UIKitCore 0x00000001af33d92c CC6E5AC7-8248-35F6-8B42-2E25C93DCF0A + 551212
9 UIKitCore 0x00000001af33d778 CC6E5AC7-8248-35F6-8B42-2E25C93DCF0A + 550776
10 UIKitCore 0x00000001af318c94 CC6E5AC7-8248-35F6-8B42-2E25C93DCF0A + 400532
11 UIKitCore 0x00000001af724114 CC6E5AC7-8248-35F6-8B42-2E25C93DCF0A + 4641044
12 UIKitCore 0x00000001af7247e8 CC6E5AC7-8248-35F6-8B42-2E25C93DCF0A + 4642792
13 UIKitCore 0x00000001af73a820 CC6E5AC7-8248-35F6-8B42-2E25C93DCF0A + 4732960
14 UIKitCore 0x00000001af732e18 CC6E5AC7-8248-35F6-8B42-2E25C93DCF0A + 4701720
15 UIKitCore 0x00000001af7363ac CC6E5AC7-8248-35F6-8B42-2E25C93DCF0A + 4715436
16 UIKitCore 0x00000001af737240 CC6E5AC7-8248-35F6-8B42-2E25C93DCF0A + 4719168
17 UIKitCore 0x00000001af737550 CC6E5AC7-8248-35F6-8B42-2E25C93DCF0A + 4719952
18 UIKitCore 0x00000001af737190 CC6E5AC7-8248-35F6-8B42-2E25C93DCF0A + 4718992
19 UIKitCore 0x00000001af737614 CC6E5AC7-8248-35F6-8B42-2E25C93DCF0A + 4720148
20 ntpc 0x00000001030dce88 ntpc + 1429128
21 AVFCapture 0x00000001c6794b38 083B23EB-2DC8-3813-817B-6F1A40C9EC24 + 498488
22 AVFCapture 0x00000001c6793c60 083B23EB-2DC8-3813-817B-6F1A40C9EC24 + 494688
23 CMCapture 0x00000001c67ff5e4 BD2C0FD0-B1F9-374F-9D59-2B5266446EFF + 153060
24 CMCapture 0x00000001c6a61304 BD2C0FD0-B1F9-374F-9D59-2B5266446EFF + 2650884
25 libdispatch.dylib 0x00000001ad1b3280 AF27E74C-BE4A-3364-BB27-AED9916CE02D + 397952
26 libdispatch.dylib 0x00000001ad158390 AF27E74C-BE4A-3364-BB27-AED9916CE02D + 25488
27 libdispatch.dylib 0x00000001ad1690ac AF27E74C-BE4A-3364-BB27-AED9916CE02D + 94380
28 libdispatch.dylib 0x00000001ad15bc94 AF27E74C-BE4A-3364-BB27-AED9916CE02D + 40084
29 libdispatch.dylib 0x00000001ad15c8a8 AF27E74C-BE4A-3364-BB27-AED9916CE02D + 43176
30 libdispatch.dylib 0x00000001ad166338 AF27E74C-BE4A-3364-BB27-AED9916CE02D + 82744
31 libsystem_pthread.dylib 0x00000001f5d9d5a4 _pthread_wqthread + 272
32 libsystem_pthread.dylib 0x00000001f5da0874 start_wqthread + 8
)
2021-04-15 16:21:04.195 ntpc[5257:1741639] *** Terminating app due to uncaught exception 'NSInternalInconsistencyException',reason: 'Modifications to the layout engine must not be performed from a background thread after it has been accessed from the main thread.'
*** First throw call stack:
(0x1ad57a9d8 0x1c18fdb54 0x1c1b80640 0x1c1b80904 0x1b0204ef0 0x1b0206294 0x1af33d0a0 0x1af338880 0x1af338aec 0x1af338f7c 0x1af33d92c 0x1af33d778 0x1af318c94 0x1af724114 0x1af7247e8 0x1af73a820 0x1af732e18 0x1af7363ac 0x1af737240 0x1af737550 0x1af737190 0x1af737614 0x1030dce88 0x1c6794b38 0x1c6793c60 0x1c67ff5e4 0x1c6a61304 0x1ad1b3280 0x1ad158390 0x1ad1690ac 0x1ad15bc94 0x1ad15c8a8 0x1ad166338 0x1f5d9d5a4 0x1f5da0874)
libc++abi.dylib: terminating with uncaught exception of type NSException
请帮忙....
解决方法
正如错误消息所说,您尝试在后台线程上修改布局:
This application is modifying the autolayout engine from a background thread
您必须在主线程上显示警报控制器:
dispatch_async(dispatch_get_main_queue(),^{
[self presentViewController:alert animated:YES completion:nil]
});
,
与 UI 的所有交互都必须在主线程中完成,因此您需要将其包装在主线程中的调度中,即:
dispatch_async(dispatch_get_main_queue(),^{
UIAlertController * alert = [UIAlertController alertControllerWithTitle:@"QRCode is Scanned" message:@"" preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction* ok = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault handler:^(UIAlertAction * action) {
[self.delegate addItemViewController:self didFinishEnteringItem:codeString];
[self.navigationController popViewControllerAnimated:YES];
}];
[alert addAction:ok];
[self presentViewController:alert animated:YES completion:nil];
});
另一方面,这次崩溃不是真的。这是由 Xcode 默认启用的主线程检查器引起的。我建议您保持启用状态。
您可以在此链接中找到更多信息 Main Thread Checker
,来自日志 import os
os.startfile(os.getenv('APPDATA') + r'\Zoom\bin_00\Zoom.exe')
您可以打印它是否在主线程中。如果没有,您可以将代码更改为主线程。因为只有主线程可以更改和布局 UIView。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。