在我的OS X中使用swift遇到了很多这个错误:
“这个应用程序正在从后台线程修改自动布局引擎,这可能会导致引擎损坏和奇怪的崩溃。这将在未来的版本中导致异常。”
我有一个NSWindow我将视图交换到窗口的contentView。当我尝试执行NSApp时会得到错误。或者当我向窗口添加子视图时。尝试禁用自动调整大小的东西,我没有任何使用自动布局的东西。任何想法吗?
有时它很好,什么都没有发生,其他时候它完全破坏了我的UI,什么都没有加载
在我的OS X中使用swift遇到了很多这个错误:
“这个应用程序正在从后台线程修改自动布局引擎,这可能会导致引擎损坏和奇怪的崩溃。这将在未来的版本中导致异常。”
我有一个NSWindow我将视图交换到窗口的contentView。当我尝试执行NSApp时会得到错误。或者当我向窗口添加子视图时。尝试禁用自动调整大小的东西,我没有任何使用自动布局的东西。任何想法吗?
有时它很好,什么都没有发生,其他时候它完全破坏了我的UI,什么都没有加载
当前回答
我有同样的问题时,试图更新错误消息在UILabel在同一个ViewController(它需要一点时间来更新数据时,试图这样做的正常编码)。我在Swift 3 Xcode 8中使用了DispatchQueue,它可以工作。
其他回答
对我来说,问题是这样的。 确保在主线程上执行performSegueWithIdentifier::
dispatch_async (dispatch_get_main_queue(), ^{
[self performSegueWithIdentifier:@"ViewController" sender:nil];
});
当您尝试更新文本字段值或在后台线程中添加子视图时,可能会遇到此问题。因此,应该将这类代码放在主线程中。
您需要用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"
}
}
我在UITableView中重新加载数据时遇到了这个问题。简单调度重新加载如下固定的问题为我。
dispatch_async(dispatch_get_main_queue(), { () -> Void in
self.tableView.reloadData()
})
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.
有同样的问题,因为我正在使用performSelectorInBackground。