在我的OS X中使用swift遇到了很多这个错误:
“这个应用程序正在从后台线程修改自动布局引擎,这可能会导致引擎损坏和奇怪的崩溃。这将在未来的版本中导致异常。”
我有一个NSWindow我将视图交换到窗口的contentView。当我尝试执行NSApp时会得到错误。或者当我向窗口添加子视图时。尝试禁用自动调整大小的东西,我没有任何使用自动布局的东西。任何想法吗?
有时它很好,什么都没有发生,其他时候它完全破坏了我的UI,什么都没有加载
在我的OS X中使用swift遇到了很多这个错误:
“这个应用程序正在从后台线程修改自动布局引擎,这可能会导致引擎损坏和奇怪的崩溃。这将在未来的版本中导致异常。”
我有一个NSWindow我将视图交换到窗口的contentView。当我尝试执行NSApp时会得到错误。或者当我向窗口添加子视图时。尝试禁用自动调整大小的东西,我没有任何使用自动布局的东西。任何想法吗?
有时它很好,什么都没有发生,其他时候它完全破坏了我的UI,什么都没有加载
当前回答
我在UITableView中重新加载数据时遇到了这个问题。简单调度重新加载如下固定的问题为我。
dispatch_async(dispatch_get_main_queue(), { () -> Void in
self.tableView.reloadData()
})
其他回答
“此应用程序正在从后台线程修改自动布局引擎”错误在实际问题发生后很长时间才会记录在控制台中,因此如果不使用断点,调试这个错误可能会很困难。
我使用@markussvensson的答案来检测我的问题,并使用这个符号断点(调试>断点>创建符号断点)发现它:
符号:[UIView layoutIfNeeded]或[UIView updateConstraintsIfNeeded] 条件:!(BOOL)[NSThread是主线程]
在模拟器上构建和运行应用程序,并复制导致抛出错误消息的步骤(应用程序将比平时慢!)然后Xcode将停止应用程序并标记从后台线程访问UI的代码行(例如func的调用)。
如果要查找此错误,请使用主线程检查器暂停问题复选框。 在大多数情况下,修复它很容易,只需将有问题的行分派到主队列中即可。
对我来说,问题是这样的。 确保在主线程上执行performSegueWithIdentifier::
dispatch_async (dispatch_get_main_queue(), ^{
[self performSegueWithIdentifier:@"ViewController" sender:nil];
});
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.
当我在一个NSURLConnection异步请求完成处理程序中调用一个做UI更新的块时,我有这个问题,因为更新到iOS 9 SDK。使用dispatch_main_queue将块调用放在dispatch_async中解决了这个问题。
它在iOS 8中运行良好。