在我的OS X中使用swift遇到了很多这个错误:
“这个应用程序正在从后台线程修改自动布局引擎,这可能会导致引擎损坏和奇怪的崩溃。这将在未来的版本中导致异常。”
我有一个NSWindow我将视图交换到窗口的contentView。当我尝试执行NSApp时会得到错误。或者当我向窗口添加子视图时。尝试禁用自动调整大小的东西,我没有任何使用自动布局的东西。任何想法吗?
有时它很好,什么都没有发生,其他时候它完全破坏了我的UI,什么都没有加载
在我的OS X中使用swift遇到了很多这个错误:
“这个应用程序正在从后台线程修改自动布局引擎,这可能会导致引擎损坏和奇怪的崩溃。这将在未来的版本中导致异常。”
我有一个NSWindow我将视图交换到窗口的contentView。当我尝试执行NSApp时会得到错误。或者当我向窗口添加子视图时。尝试禁用自动调整大小的东西,我没有任何使用自动布局的东西。任何想法吗?
有时它很好,什么都没有发生,其他时候它完全破坏了我的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.
其他回答
它需要放在一个不同的线程中,允许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
});
斯威夫特4,
假设,如果您正在使用操作队列调用某个方法
operationQueue.addOperation({
self.searchFavourites()
})
假设函数searchfavorites是这样的,
func searchFavourites() {
DispatchQueue.main.async {
//Your code
}
}
如果你在主线程中调用“searchfavorites”方法中的所有代码,如果你在其中更新一些UI,它仍然会给出一个错误。
这个应用程序正在从后台修改自动布局引擎 从主线程访问引擎后的线程。
所以使用溶液,
operationQueue.addOperation({
DispatchQueue.main.async {
self.searchFavourites()
}
})
对于这种情况。
看看日志里的这一行
$S12AppName18ViewControllerC11Func()ySS_S2StF + 4420
你可以检查哪个函数从后台线程调用,或者你在哪里调用API方法,你需要像这样从主线程调用你的函数。
DispatchQueue.main.async { func()}
Func()是你想在API调用的结果中调用的函数 要么成功,要么失败。
日志在这里
This application is modifying the autolayout engine from a background thread after the engine was accessed from the main thread. This can lead to engine corruption and weird crashes.
Stack:(
0 Foundation 0x00000001c570ce50 <redacted> + 96
1 Foundation 0x00000001c5501868 <redacted> + 32
2 Foundation 0x00000001c5544370 <redacted> + 540
3 Foundation 0x00000001c5543840 <redacted> + 396
4 Foundation 0x00000001c554358c <redacted> + 272
5 Foundation 0x00000001c5542e10 <redacted> + 264
6 UIKitCore 0x00000001f20d62e4 <redacted> + 488
7 UIKitCore 0x00000001f20d67b0 <redacted> + 36
8 UIKitCore 0x00000001f20d6eb0 <redacted> + 84
9 Foundation 0x00000001c571d124 <redacted> + 76
10 Foundation 0x00000001c54ff30c <redacted> + 108
11 Foundation 0x00000001c54fe304 <redacted> + 328
12 UIKitCore 0x00000001f151dc0c <redacted> + 156
13 UIKitCore 0x00000001f151e0c0 <redacted> + 152
14 UIKitCore 0x00000001f1514834 <redacted> + 868
15 UIKitCore 0x00000001f1518760 <redacted> + 104
16 UIKitCore 0x00000001f1543370 <redacted> + 1772
17 UIKitCore 0x00000001f1546598 <redacted> + 120
18 UIKitCore 0x00000001f14fc850 <redacted> + 1452
19 UIKitCore 0x00000001f168f318 <redacted> + 196
20 UIKitCore 0x00000001f168d330 <redacted> + 144
21 AppName 0x0000000100b8ed00 $S12AppName18ViewControllerC11Func()ySS_S2StF + 4420
22 AppName 0x0000000100b8d9f4 $S12CcfU0_y10Foundation4DataVSg_So13NSURLResponseCSgs5Error_pSgtcfU_ + 2384
23 App NAme 0x0000000100a98f3c $S10Foundation4DataVSgSo13NSURLResponseCSgs5Error_pSgIegggg_So6NSDataCSgAGSo7NSErrorCSgIeyByyy_TR + 316
24 CFNetwork 0x00000001c513aa00 <redacted> + 32
25 CFNetwork 0x00000001c514f1a0 <redacted> + 176
26 Foundation 0x00000001c55ed8bc <redacted> + 16
27 Foundation 0x00000001c54f5ab8 <redacted> + 72
28 Foundation 0x00000001c54f4f8c <redacted> + 740
29 Foundation 0x00000001c55ef790 <redacted> + 272
30 libdispatch.dylib 0x000000010286f824 _dispatch_call_block_and_release + 24
31 libdispatch.dylib 0x0000000102870dc8 _dispatch_client_callout + 16
32 libdispatch.dylib 0x00000001028741c4 _dispatch_continuation_pop + 528
33 libdispatch.dylib 0x0000000102873604 _dispatch_async_redirect_invoke + 632
34 libdispatch.dylib 0x00000001028821dc _dispatch_root_queue_drain + 376
35 libdispatch.dylib 0x0000000102882bc8 _dispatch_worker_thread2 + 156
36 libsystem_pthread.dylib 0x00000001c477917c _pthread_wqthread + 472
37 libsystem_pthread.dylib 0x00000001c477bcec start_wqthread + 4
)
如果要查找此错误,请使用主线程检查器暂停问题复选框。 在大多数情况下,修复它很容易,只需将有问题的行分派到主队列中即可。
“这个应用程序正在从后台线程修改自动布局引擎”的主要问题是,它似乎在实际问题发生后很长一段时间才被记录下来,这可能会使它很难排除故障。
我设法通过创建三个符号断点来解决这个问题。
调试>断点>创建符号断点…
断点1:
符号:-[UIView setNeedsLayout] 条件:!(BOOL)[NSThread是主线程]
断点2:
符号:-[UIView layoutIfNeeded] 条件:!(BOOL)[NSThread是主线程]
断点3:
符号:-[UIView updateConstraintsIfNeeded] 条件:!(BOOL)[NSThread是主线程]
使用这些断点,您可以很容易地在非主线程上错误地调用UI方法的实际行中获得断点。