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

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

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

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


当前回答

I also encountered this problem, seeing a ton of these messages and stack traces being printed in the output, when I resized the window to a smaller size than its initial value. Spending a long time figuring out the problem, I thought I'd share the rather simple solution. I had once enabled Can Draw Concurrently on an NSTextView through IB. That tells AppKit that it can call the view's draw(_:) method from another thread. After disabling it, I no longer got any error messages. I didn't experience any problems before updating to macOS 10.14 Beta, but at the same time, I also started modifying the code to perform work with the text view.

其他回答

显然你正在后台线程上做一些UI更新。在没有看到代码的情况下,无法准确预测位置。

以下是一些可能发生的情况:-

你可能正在后台线程上做一些事情,但没有使用。在同一个函数中,这段代码更容易被发现。

DispatchQueue.main.async { // do UI update here }

在后台线程调用func进行web请求调用,它的完成处理程序调用其他func进行UI更新。 要解决这个问题,请尝试检查您在webrequest调用后更新UI的代码。

// Do something on background thread
DispatchQueue.global(qos: .userInitiated).async {
   // update UI on main thread
   DispatchQueue.main.async {
                // Updating whole table view
                self.myTableview.reloadData()
            }
}

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

您需要用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"
   }
}

我也有同样的问题。结果我使用的是需要主队列的UIAlerts。但是,他们已经被弃用了。 当我将UIAlerts更改为UIAlertController时,我不再有这个问题,也不必使用任何dispatch_async代码。教训是,注意警告。他们会在你意想不到的时候帮助你。

对我来说,这个错误消息来自Admob SDK的一个横幅。

我可以通过设置一个条件断点来跟踪原点到“WebThread”。

然后我就可以通过封装Banner的创建来摆脱这个问题:

dispatch_async(dispatch_get_main_queue(), ^{
   _bannerForTableFooter = [[GADBannerView alloc] initWithAdSize:kGADAdSizeSmartBannerPortrait];
   ...
}

我不知道为什么这有帮助,因为我看不出这段代码是如何从一个非主线程调用的。

希望它能帮助到任何人。

对我来说,问题是这样的。 确保在主线程上执行performSegueWithIdentifier::

dispatch_async (dispatch_get_main_queue(), ^{
  [self performSegueWithIdentifier:@"ViewController" sender:nil];
});