app从后台线程修改autolayout引擎

Mic*_*hel 0 multithreading dispatch task-queue ios swift

我几乎完成了将我的iOS应用程序迁移到Swift 3.0.但我仍然有一些类似于下面的情况.其中大部分我都能通过将有问题的代码放在主线程上来解决.

在其他一些情况下,我无法弄清楚,我的代码的哪一部分正在错误的线程上执行.我收到这样的消息:

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   CoreFoundation                      0x000000018765a1d8 <redacted> + 148
    1   libobjc.A.dylib                     0x000000018609455c objc_exception_throw + 56
    2   CoreFoundation                      0x000000018765a108 <redacted> + 0
    3   Foundation                          0x0000000188241ea4 <redacted> + 192
    ....................
    16  libsystem_pthread.dylib             0x00000001866eece4 <redacted> + 200
    17  libsystem_pthread.dylib             0x00000001866ee378 pthread_mutex_lock + 0
    18  libsystem_pthread.dylib             0x00000001866edda4 start_wqthread + 4
)
Run Code Online (Sandbox Code Playgroud)

是否有一些特殊的技术(使用调试器时的选项或??)我可以用来跟踪progran所遵循的路径,看看这发生了什么?

Ash*_*sey 5

显然你在后台线程上做了一些UI更新.无法预测代码,无法准确预测位置.

这些是可能发生的一些情况: -

  1. 你可能在后台线程上做了一些事情而没有使用.在相同的功能中,这个代码更容易被发现.

    DispatchQueue.main.async { // do UI update here }
    
    Run Code Online (Sandbox Code Playgroud)
  2. 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()
            }
}
Run Code Online (Sandbox Code Playgroud)