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

标签 ios swift uitableview ios9 xcode7

let url = NSURL(string: "http://api.mdec.club:3500/news?")

let task = NSURLSession.sharedSession().dataTaskWithURL(url!) {
    (data, response, error) in

    self.jsonResult = try! NSJSONSerialization.JSONObjectWithData(data!, options: NSJSONReadingOptions.MutableContainers) as! NSMutableArray

    self.tableView.reloadData()
}

task.resume()

我不明白为什么会出现此错误。我必须将 reloadData() 方法放在 task 中,因为我必须等到获取数据。我还能如何重新加载 TableView 而不会出现此错误?

最佳答案

How else can I reload the table view without getting this error?

为了调用 reloadData,您跳出到主线程,如下所示:

let task = NSURLSession.sharedSession().dataTaskWithURL(url!) {
    (data, response, error) in
    self.jsonResult = try! NSJSONSerialization.JSONObjectWithData(data!, options: NSJSONReadingOptions.MutableContainers) as! NSMutableArray
    dispatch_async(dispatch_get_main_queue()) {
        self.tableView.reloadData()
    }
}

在您从后台线程调用的代码中触摸界面时,请始终随时这样做。除了在主线程上,你永远不能永远永远不要接触界面。

关于ios - 此应用程序正在从后台线程修改自动布局引擎 - ios 9,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33715819/

相关文章:

ios - DispatchQueue.main.async 不调用转义闭包

iphone - 当我上下滚动时,UITableView 仅显示来自 JSON 的数据

iphone - uitableviewcell 交替背景单元格颜色,即使对于没有数据的单元格也是如此

ios - iOS 中的 GPUImage 二值化

ios - NSURLConnectionDownloadDelegate 文件问题

ios - 带有 UILocalizedIndexedCollat​​ion 的 Swift 选择器

ios - SCNNode childNodes 的总高度

ios - 禁用 tableview 的滚动然后 textView 成为第一响应者

ios - “Safari cannot open the page because the address is invalid” 在卸载应用程序的情况下访问 Branch 链接时出现

ios - 如何检测我的应用程序无法在 iOS 6 中访问用户的 Twitter 帐户?