在我的OS X中使用swift遇到了很多这个错误:

“这个应用程序正在从后台线程修改自动布局引擎,这可能会导致引擎损坏和奇怪的崩溃。这将在未来的版本中导致异常。”

我有一个NSWindow我将视图交换到窗口的contentView。当我尝试执行NSApp时会得到错误。或者当我向窗口添加子视图时。尝试禁用自动调整大小的东西,我没有任何使用自动布局的东西。任何想法吗?

有时它很好,什么都没有发生,其他时候它完全破坏了我的UI,什么都没有加载


当前回答

你不能在主线程中越位修改UI !UIKit不是线程安全的,所以如果你这样做,上面的问题和其他一些奇怪的问题会出现。应用程序甚至会崩溃。

所以,要做UIKit操作,你需要定义block并让它在主队列上执行:例如,

NSOperationQueue.mainQueue().addOperationWithBlock {

}

其他回答

“此应用程序正在从后台线程修改自动布局引擎”错误在实际问题发生后很长时间才会记录在控制台中,因此如果不使用断点,调试这个错误可能会很困难。

我使用@markussvensson的答案来检测我的问题,并使用这个符号断点(调试>断点>创建符号断点)发现它:

符号:[UIView layoutIfNeeded]或[UIView updateConstraintsIfNeeded] 条件:!(BOOL)[NSThread是主线程]

在模拟器上构建和运行应用程序,并复制导致抛出错误消息的步骤(应用程序将比平时慢!)然后Xcode将停止应用程序并标记从后台线程访问UI的代码行(例如func的调用)。

它可以像设置文本字段/标签值或在后台线程中添加子视图一样简单,这可能会导致字段的布局发生变化。确保你对接口所做的任何事情都只发生在主线程中。

查看这个链接:https://forums.developer.apple.com/thread/7399

当您尝试更新文本字段值或在后台线程中添加子视图时,可能会遇到此问题。因此,应该将这类代码放在主线程中。

您需要用dispatch_asynch包装调用UI更新的方法以获得主队列。例如:

dispatch_async(dispatch_get_main_queue(), { () -> Void in
   self.friendLabel.text = "You are following \(friendCount) accounts"
})

编辑- swift 3:

现在,我们可以按照下面的代码来做:

// Move to a background thread to do some long running work
DispatchQueue.global(qos: .userInitiated).async {
   // Do long running task here
   // Bounce back to the main thread to update the UI
   DispatchQueue.main.async {
      self.friendLabel.text = "You are following \(friendCount) accounts"
   }
}

You already have the correct code answer from @Mark but, just to share my findings: The issue is that you are requesting a change in the view and assuming that it will happen instantly. In reality, the loading of a view depends on the available resources. If everything loads quickly enough and there are no delays then you don't notice anything. In scenarios, where there is any delay due to the process thread being busy etc, the application runs into a situation where it is supposed to display something even though its not ready yet. Hence, it is advisable to dispatch these requests in a asynchronous queues so, they get executed based on the load.

在不使用'dispatch_async'的情况下调试print语句时会得到类似的错误消息 因此,当您得到错误消息时,就可以使用了

斯威夫特4

DispatchQueue.main.async { //code }

斯威夫特3

DispatchQueue.main.async(){ //code }

早期的Swift版本

dispatch_async(dispatch_get_main_queue()){ //code }