iOS 14 上的 Objective-C UIAlertController 崩溃

如何解决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 举报,一经查实,本站将立刻删除。

相关推荐


依赖报错 idea导入项目后依赖报错,解决方案:https://blog.csdn.net/weixin_42420249/article/details/81191861 依赖版本报错:更换其他版本 无法下载依赖可参考:https://blog.csdn.net/weixin_42628809/a
错误1:代码生成器依赖和mybatis依赖冲突 启动项目时报错如下 2021-12-03 13:33:33.927 ERROR 7228 [ main] o.s.b.d.LoggingFailureAnalysisReporter : *************************** APPL
错误1:gradle项目控制台输出为乱码 # 解决方案:https://blog.csdn.net/weixin_43501566/article/details/112482302 # 在gradle-wrapper.properties 添加以下内容 org.gradle.jvmargs=-Df
错误还原:在查询的过程中,传入的workType为0时,该条件不起作用 <select id="xxx"> SELECT di.id, di.name, di.work_type, di.updated... <where> <if test=&qu
报错如下,gcc版本太低 ^ server.c:5346:31: 错误:‘struct redisServer’没有名为‘server_cpulist’的成员 redisSetCpuAffinity(server.server_cpulist); ^ server.c: 在函数‘hasActiveC
解决方案1 1、改项目中.idea/workspace.xml配置文件,增加dynamic.classpath参数 2、搜索PropertiesComponent,添加如下 <property name="dynamic.classpath" value="tru
删除根组件app.vue中的默认代码后报错:Module Error (from ./node_modules/eslint-loader/index.js): 解决方案:关闭ESlint代码检测,在项目根目录创建vue.config.js,在文件中添加 module.exports = { lin
查看spark默认的python版本 [root@master day27]# pyspark /home/software/spark-2.3.4-bin-hadoop2.7/conf/spark-env.sh: line 2: /usr/local/hadoop/bin/hadoop: No s
使用本地python环境可以成功执行 import pandas as pd import matplotlib.pyplot as plt # 设置字体 plt.rcParams['font.sans-serif'] = ['SimHei'] # 能正确显示负号 p
错误1:Request method ‘DELETE‘ not supported 错误还原:controller层有一个接口,访问该接口时报错:Request method ‘DELETE‘ not supported 错误原因:没有接收到前端传入的参数,修改为如下 参考 错误2:cannot r
错误1:启动docker镜像时报错:Error response from daemon: driver failed programming external connectivity on endpoint quirky_allen 解决方法:重启docker -> systemctl r
错误1:private field ‘xxx‘ is never assigned 按Altʾnter快捷键,选择第2项 参考:https://blog.csdn.net/shi_hong_fei_hei/article/details/88814070 错误2:启动时报错,不能找到主启动类 #
报错如下,通过源不能下载,最后警告pip需升级版本 Requirement already satisfied: pip in c:\users\ychen\appdata\local\programs\python\python310\lib\site-packages (22.0.4) Coll
错误1:maven打包报错 错误还原:使用maven打包项目时报错如下 [ERROR] Failed to execute goal org.apache.maven.plugins:maven-resources-plugin:3.2.0:resources (default-resources)
错误1:服务调用时报错 服务消费者模块assess通过openFeign调用服务提供者模块hires 如下为服务提供者模块hires的控制层接口 @RestController @RequestMapping("/hires") public class FeignControl
错误1:运行项目后报如下错误 解决方案 报错2:Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.8.1:compile (default-compile) on project sb 解决方案:在pom.
参考 错误原因 过滤器或拦截器在生效时,redisTemplate还没有注入 解决方案:在注入容器时就生效 @Component //项目运行时就注入Spring容器 public class RedisBean { @Resource private RedisTemplate<String
使用vite构建项目报错 C:\Users\ychen\work>npm init @vitejs/app @vitejs/create-app is deprecated, use npm init vite instead C:\Users\ychen\AppData\Local\npm-