在我的OS X中使用swift遇到了很多这个错误:
“这个应用程序正在从后台线程修改自动布局引擎,这可能会导致引擎损坏和奇怪的崩溃。这将在未来的版本中导致异常。”
我有一个NSWindow我将视图交换到窗口的contentView。当我尝试执行NSApp时会得到错误。或者当我向窗口添加子视图时。尝试禁用自动调整大小的东西,我没有任何使用自动布局的东西。任何想法吗?
有时它很好,什么都没有发生,其他时候它完全破坏了我的UI,什么都没有加载
在我的OS X中使用swift遇到了很多这个错误:
“这个应用程序正在从后台线程修改自动布局引擎,这可能会导致引擎损坏和奇怪的崩溃。这将在未来的版本中导致异常。”
我有一个NSWindow我将视图交换到窗口的contentView。当我尝试执行NSApp时会得到错误。或者当我向窗口添加子视图时。尝试禁用自动调整大小的东西,我没有任何使用自动布局的东西。任何想法吗?
有时它很好,什么都没有发生,其他时候它完全破坏了我的UI,什么都没有加载
当前回答
对我来说,问题是这样的。 确保在主线程上执行performSegueWithIdentifier::
dispatch_async (dispatch_get_main_queue(), ^{
[self performSegueWithIdentifier:@"ViewController" sender:nil];
});
其他回答
在不使用'dispatch_async'的情况下调试print语句时会得到类似的错误消息 因此,当您得到错误消息时,就可以使用了
斯威夫特4
DispatchQueue.main.async { //code }
斯威夫特3
DispatchQueue.main.async(){ //code }
早期的Swift版本
dispatch_async(dispatch_get_main_queue()){ //code }
“这个应用程序正在从后台线程修改自动布局引擎”的主要问题是,它似乎在实际问题发生后很长一段时间才被记录下来,这可能会使它很难排除故障。
我设法通过创建三个符号断点来解决这个问题。
调试>断点>创建符号断点…
断点1:
符号:-[UIView setNeedsLayout] 条件:!(BOOL)[NSThread是主线程]
断点2:
符号:-[UIView layoutIfNeeded] 条件:!(BOOL)[NSThread是主线程]
断点3:
符号:-[UIView updateConstraintsIfNeeded] 条件:!(BOOL)[NSThread是主线程]
使用这些断点,您可以很容易地在非主线程上错误地调用UI方法的实际行中获得断点。
对我来说,这个错误消息来自Admob SDK的一个横幅。
我可以通过设置一个条件断点来跟踪原点到“WebThread”。
然后我就可以通过封装Banner的创建来摆脱这个问题:
dispatch_async(dispatch_get_main_queue(), ^{
_bannerForTableFooter = [[GADBannerView alloc] initWithAdSize:kGADAdSizeSmartBannerPortrait];
...
}
我不知道为什么这有帮助,因为我看不出这段代码是如何从一个非主线程调用的。
希望它能帮助到任何人。
它需要放在一个不同的线程中,允许UI在线程函数执行完成后立即更新:
现代迅速:
DispatchQueue.main.async {
// Update UI
}
旧版本的Swift,在Swift 3之前。
dispatch_async(dispatch_get_main_queue(){
// code here
})
objective - c:
dispatch_async(dispatch_get_main_queue(), ^{
// code here
});
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.