简体   繁体   English

Objective-C UIAlertController 在 iOS 14 上崩溃

[英]Objective-C UIAlertController Crash on iOS 14

I have a viewcontroller functioning as QRCode scanner, and the below is the part about the scan-output.我有一个用作 QRCode 扫描仪的视图控制器,下面是关于扫描输出的部分。 It works well on iOS 12/13, but only crashed on iOS 14.它在 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];

and then the crash log came out as below, and this was ocurred on the code as "UIAlertController * alert", I think there might be something wrong for iOS 14.然后崩溃日志如下所示,这发生在代码上为“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

Please help....请帮忙....

As the error message says you try to modify the layout on a background thread: This application is modifying the autolayout engine from a background thread正如错误消息所说,您尝试在后台线程上修改布局: This application is modifying the autolayout engine from a background thread

You have to present the alert controller on the main thread:您必须在主线程上显示警报 controller:

dispatch_async(dispatch_get_main_queue(), ^{
   [self presentViewController:alert animated:YES completion:nil]
});

All interactions with the UI must be done in the Main Thread so you need to wrap it in a dispatch in the main thread, ie:与 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];
});

On the other hand, this crash is not real.另一方面,这种崩溃并不是真实的。 It is caused by the Main Thread checker that Xcode has enabled by default.主线程检查器导致 Xcode 默认启用。 I recommend you to keep it enabled.我建议您保持启用它。

You can find more information in this link Main Thread Checker您可以在此链接中找到更多信息Main Thread Checker

from the log reason: 'Modifications to the layout engine must not be performed from a background thread after it has been accessed from the main thread.'来自日志reason: 'Modifications to the layout engine must not be performed from a background thread after it has been accessed from the main thread.' You can print whether it's in the main thread.您可以打印它是否在主线程中。 If not, You can change the code to main thread.如果没有,您可以将代码更改为主线程。 because only the main thread can change and layout the UIView.因为只有主线程可以更改和布局 UIView。

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

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