首页 > 代码库 > iOS之崩溃处理:This application is modifying the autolayout engine from a background thread

iOS之崩溃处理:This application is modifying the autolayout engine from a background thread

一、错误提示

今天在开发的时候遇到一个崩溃问题,“This application is modifying the autolayout engine from a background thread, which can lead to engine corruption and weird crashes.  This will cause an exception in a future release.”如图所示

技术分享

二、错误写法

先来看一下代码的写法,如图所示:

技术分享

三、错误原因

后来观察代码后发现居然是在子线程里更新了UI导致的崩溃。在我们学习多线程的时候肯定会看到一句话:UI一定要在主线程中更新。这就是导致崩溃的主要原因,写代码GCD相关的代码一定切记更新UI的操作一定要放在主线程操作。

四、解决方法

既然知道了要在主线程更新UI,那么只需要在原来的代码里加上这段代码:

技术分享

 

 

iOS之崩溃处理:This application is modifying the autolayout engine from a background thread