在我的OS X中使用swift遇到了很多这个错误:
“这个应用程序正在从后台线程修改自动布局引擎,这可能会导致引擎损坏和奇怪的崩溃。这将在未来的版本中导致异常。”
我有一个NSWindow我将视图交换到窗口的contentView。当我尝试执行NSApp时会得到错误。或者当我向窗口添加子视图时。尝试禁用自动调整大小的东西,我没有任何使用自动布局的东西。任何想法吗?
有时它很好,什么都没有发生,其他时候它完全破坏了我的UI,什么都没有加载
在我的OS X中使用swift遇到了很多这个错误:
“这个应用程序正在从后台线程修改自动布局引擎,这可能会导致引擎损坏和奇怪的崩溃。这将在未来的版本中导致异常。”
我有一个NSWindow我将视图交换到窗口的contentView。当我尝试执行NSApp时会得到错误。或者当我向窗口添加子视图时。尝试禁用自动调整大小的东西,我没有任何使用自动布局的东西。任何想法吗?
有时它很好,什么都没有发生,其他时候它完全破坏了我的UI,什么都没有加载
当前回答
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.
其他回答
显然你正在后台线程上做一些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()
}
}
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.
它可以像设置文本字段/标签值或在后台线程中添加子视图一样简单,这可能会导致字段的布局发生变化。确保你对接口所做的任何事情都只发生在主线程中。
查看这个链接:https://forums.developer.apple.com/thread/7399
对我来说,问题是这样的。 确保在主线程上执行performSegueWithIdentifier::
dispatch_async (dispatch_get_main_queue(), ^{
[self performSegueWithIdentifier:@"ViewController" sender:nil];
});
当我在一个NSURLConnection异步请求完成处理程序中调用一个做UI更新的块时,我有这个问题,因为更新到iOS 9 SDK。使用dispatch_main_queue将块调用放在dispatch_async中解决了这个问题。
它在iOS 8中运行良好。