iOS 遇到的问题

前言

在工作中遇到一个问题.从来没有遇到过.拿出来分享给大家.给出的crash信息是:
accessing _cachedSystemAnimationFence requires the main thread 应该放到主线程中进行
出现的场景是在获取照相机权限的时候:

[PHPhotoLibrary requestAuthorization:^(PHAuthorizationStatus status){
    if(status == PHAuthorizationStatusAuthorized){
    }else if(status == PHAuthorizationStatusDenied || status == PHAuthorizationStatusRestricted){
        UIAlertController *alertVC = [UIAlertController alertControllerWithTitle:nil message:@"*************" preferredStyle:UIAlertControllerStyleAlert];
        [alertVC addAction:[UIAlertAction actionWithTitle:@"**" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonull action){}]];
        [self.navigationController presentViewController:alertVC animated:true completion:nil];
    }
    return;
} ]
//然后项目崩到 presentViewController: animated: completion:里打印当前线程发现确实不是main主线程

解决方法比较简单:

dispatch_async(dispatch_get_main_queue(),^{
    [self.navigationController presentViewController:alertVC animated:true completion:nil];
});

目前测试是没有问题的.我也能明白为啥要放到主线程去,毕竟所有关于UI的操作都应该放到主线程中去进行.没毛病!但是!!! 为啥我在普通的controller里写了这么久一点问题都没有.然而放到webView里就出现了这么NB的错误,由于以前没有遇到过所以查了一些资料,惊奇的发现貌似很多这个问题都出现在webView出现的时候.例如前辈的经验之谈貌似和我出现的问题很相似!
但是依然没有提到为啥有这种问题.后来经过个人实践发现:
1.滑动移动放大缩小等手势.不会出现崩溃,目前发现的只有点击事件
2.webView都有的问题而非h5页面有问题.套入百度页面等点击之后都会崩溃.
所以为啥呢.在stackoverflow上看到一个前辈问题里边的解释是:
If we need to call any UI Related operation inside the block use dispatch. Everything that interacts with the UI must be run on the main thread. You can run other tasks that don’t relate to the UI outside the main thread to increase performance.
但是我还是不明白为啥别的页面没有毛病,接下来看到了一个Jira问题咨询
重点摘取
通过第一个地址发现.这个问题发生在iOS9.0以后webview的一个crash.然后我做了测试..确实如此.我使用的是iOS8.6 iphone5s确实没有出现之前的崩溃问题.那应该就是iOS9.0以后SDK的更改问题.,第一个地址里.貌似给出了html5 的解决方案.对h5 并不是特别熟悉.没办法给与解答.
探讨到了这一步.有点难以进行下去,最后去官方的document看了看,在Photos下的PHPhotoLibrary中的requestAuthorization里有这样一个
Note: Photos may call your handler block on an arbitrary serial queue.If your handler needs to interact with UI elements,dispatch such work to the main queue.
那就听apple的话按章办事吧.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值