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

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

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

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


当前回答

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

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

NSOperationQueue.mainQueue().addOperationWithBlock {

}

其他回答

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.

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

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

当我在一个NSURLConnection异步请求完成处理程序中调用一个做UI更新的块时,我有这个问题,因为更新到iOS 9 SDK。使用dispatch_main_queue将块调用放在dispatch_async中解决了这个问题。

它在iOS 8中运行良好。

斯威夫特4,

假设,如果您正在使用操作队列调用某个方法

operationQueue.addOperation({
            self.searchFavourites()
        })

假设函数searchfavorites是这样的,

func searchFavourites() {
     DispatchQueue.main.async {
                    //Your code
                }
}

如果你在主线程中调用“searchfavorites”方法中的所有代码,如果你在其中更新一些UI,它仍然会给出一个错误。

这个应用程序正在从后台修改自动布局引擎 从主线程访问引擎后的线程。

所以使用溶液,

operationQueue.addOperation({
            DispatchQueue.main.async {
                self.searchFavourites()
            }
        })

对于这种情况。

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

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