且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

此应用程序正在从后台线程修改自动布局引擎,这可能导致引擎损坏

更新时间:2022-12-09 11:10:16

dataTaskWithRequest调用在后台运行,然后从同一线程调用完成处理程序.任何更新UI的操作都应在主线程上运行,因此所有当前的处理程序代码应在dispatch_async之内,并返回主队列:

The dataTaskWithRequest call runs in the background and then calls your completion handler from the same thread. Anything that updates the UI should run on the main thread, so all of your current handler code should be within a dispatch_async back onto the main queue:

dispatch_async(dispatch_get_main_queue()) {
  // Do stuff to UI
}

快捷键3:

DispatchQueue.main.async() {
  // Do stuff to UI
}

因此,理想情况下,当前在if error == nil中拥有的所有代码都应该在另一个函数(称为handleRequest)中关闭,这样您的当前代码将变为:

Therefore, ideally all the code you currently have within if error == nil should be off in another function, say called handleRequest, so your current code becomes:

session.dataTaskWithRequest(request, completionHandler: {(data, response, error) in
    if error == nil {
        dispatch_async(dispatch_get_main_queue(), {
            self.handleRequest(...)I
        })
    }