ios iOS9 - 此应用程序正在从后台线程修改自动布局引擎 - 在哪里?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/32680303/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 07:44:48  来源:igfitidea点击:

iOS9 - This application is modifying the autolayout engine from a background thread -- where?

iosobjective-cxcode7

提问by robm

For searching, the error message is:

对于搜索,错误消息是:

This application is modifying the autolayout engine from a background thread, which can lead to engine corruption and weird crashes. This will cause an exception in a future release.

此应用程序正在从后台线程修改自动布局引擎,这可能导致引擎损坏和奇怪的崩溃。这将在未来版本中导致异常。

I know this means some UIKit code is called from a background thread, and I know the solution is to wrap the code in

我知道这意味着一些 UIKit 代码是从后台线程调用的,我知道解决方案是将代码包装在

dispatch_async(dispatch_get_main_queue(), ^(void){ <code> });

dispatch_async(dispatch_get_main_queue(), ^(void){ <code> });

My problem is locating where to do that, as none of the printed stack traces reference my app code. My evidence to prove this negative is searching (command-f) in the debug output window for the name of my app; I have 24 stack traces dumped out and my app name is not in any of them, except at the top with the error message. I can post one of them, but that doesn't seem very useful.

我的问题是找到在哪里执行此操作,因为没有打印的堆栈跟踪引用我的应用程序代码。我证明这个否定的证据是在调试输出窗口中搜索(command-f)我的应用程序的名称;我转储了 24 个堆栈跟踪,我的应用程序名称不在其中任何一个中,除了在顶部带有错误消息。我可以发布其中之一,但这似乎不是很有用。

In the cases I am working on today, this is happening when transitioning view controllers, after viewWillDisappear()and before viewWillAppear(). I have found parts of my code to wrap dispatch_async()around, but those are all handled now. I have breakpoints and debug messages where objects relevant to the view controllers are allocated and deallocated, and they all trigger after the exception messages appear. This is happening both on the simulator and my iOS9 iPhone, both in debug and release modes.

在我今天处理的情况下,这发生在转换视图控制器时,之后viewWillDisappear()和之前viewWillAppear()。我找到了我的代码的一部分来环绕dispatch_async(),但现在这些都被处理了。我有断点和调试消息,其中分配和释放与视图控制器相关的对象,它们都在异常消息出现后触发。这发生在模拟器和我的 iOS9 iPhone 上,在调试和发布模式下。

How can I identify the background code which is apparently modifying the UI?

如何识别明显修改 UI 的背景代码?

回答by Mahmoud Adam

This code PSPDFUIKitMainThreadGuardcauses assertions on UIKit access outside the main thread

此代码PSPDFUIKitMainThreadGuard导致对主线程外 UIKit 访问的断言

Steps to use:

使用步骤:

  1. Add to project and compile this file without ARC
  2. Move PSPDFAssertdefinition to the first of the file
  3. Comment calling of PSPDFLogErroras it is not defined
  4. import <UIKit/UIKit.h>
  1. 添加到项目并在没有 ARC 的情况下编译此文件
  2. PSPDFAssert定义移动到文件的第一个
  3. PSPDFLogError未定义的注释调用
  4. 进口 <UIKit/UIKit.h>

Your app will crash and stop with any attemption to modify any UI element from background thread

您的应用程序将崩溃并停止尝试从后台线程修改任何 UI 元素

For swift use the following code: NBUIKitMainThreadGuard

对于快速使用以下代码:NBUIKitMainThreadGuard

回答by Ashley Mills

Edit your project scheme, and under the Runaction, in the Diagnosticstab, under Runtime API Checking, make sure Pause in issuesis checked. When any background UI updates occur, the app will pause.

编辑您的项目方案,并在Run操作下的Diagnostics选项卡中,在Runtime API Checking 下,确保选中Pause in issues。当发生任何后台 UI 更新时,应用程序将暂停。

enter image description here

在此处输入图片说明

回答by RodolfoNeto

NSOperationQueue.mainQueue().addOperationWithBlock(){
[YOUR CODE]
}