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

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

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

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


当前回答

“这个应用程序正在从后台线程修改自动布局引擎”的主要问题是,它似乎在实际问题发生后很长一段时间才被记录下来,这可能会使它很难排除故障。

我设法通过创建三个符号断点来解决这个问题。

调试>断点>创建符号断点…

断点1:

符号:-[UIView setNeedsLayout] 条件:!(BOOL)[NSThread是主线程]

断点2:

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

断点3:

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

使用这些断点,您可以很容易地在非主线程上错误地调用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.

斯威夫特4,

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

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

假设函数searchfavorites是这样的,

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

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

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

所以使用溶液,

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

对于这种情况。

如果要查找此错误,请使用主线程检查器暂停问题复选框。 在大多数情况下,修复它很容易,只需将有问题的行分派到主队列中即可。

显然你正在后台线程上做一些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()
            }
}

我在UITableView中重新加载数据时遇到了这个问题。简单调度重新加载如下固定的问题为我。

    dispatch_async(dispatch_get_main_queue(), { () -> Void in
        self.tableView.reloadData()
    })