【问题标题】:Changing UI from a background thread从后台线程更改 UI
【发布时间】:2016-04-10 12:32:32
【问题描述】:

我收到一个错误,我正在从后台线程更改自动布局引擎,有没有办法知道哪个代码块发出了这个警告?还是我自己去找?? 谢谢。

【问题讨论】:

  • 您可以像这样在 xCode 中设置断点,也许它会显示代码块发出警告。 enter image description here
  • 如果您要更新代码中的约束,只需将它们放在 NSOperationQueue mainQueue 块下。

标签: ios xcode7 ui-thread background-thread


【解决方案1】:

可能有更好的答案,但我会先把

对于斯威夫特:

assert(NSThread.isMainThread(), "Expected code to be called on main thread");

对于 Objective-C:

NSAssert([NSThread isMainThread], @"Expected code to be called on main thread"); 

到您怀疑的所有代码部分。

如果您不在主线程上执行该代码,这样应用程序将会崩溃。


这个错误是错误还是异常?如果是异常,您可以尝试使用异常断点来捕获它,如下所示:How to debug “Collection was mutated while being enumerated” errors and like, when Xcode does not provide me with enough information??


查看您的确切消息后,您需要的是:

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

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

【讨论】:

  • 我有一个将近 30K 行代码的项目:D,我做不到
  • 这个错误是错误还是异常?如果是异常,您是否尝试使用 here 之类的异常断点来捕获它?
  • 这是一个警告,系统会在 xcode 的控制台上打印:此应用程序正在从后台线程修改自动布局引擎,这可能导致引擎损坏和奇怪的崩溃。这将在未来的版本中导致异常
猜你喜欢
  • 2012-04-24
  • 2020-11-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-04-05
  • 1970-01-01
  • 2014-05-28
相关资源
最近更新 更多