此应用程序正在从后台线程修改autolayout引擎 - ios9 [英] This application is modifying the autolayout engine from a background thread - ios9
问题描述
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()
方法放在任务
中,因为我必须等到获取数据。如何在不收到此错误的情况下重新加载表格视图?
I don't understand why I'm getting this error. I have to put the reloadData()
method inside task
because I have to wait till the get the data. How else can I reload the table view without getting this error?
推荐答案
我还能怎么重装表格视图没有收到此错误?
How else can I reload the table view without getting this error?
你走出主线程以便调用 reloadData
,像这样:
You step out to the main thread in order to call reloadData
, like this:
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()
}
}
在从后台线程调用的代码中触摸界面时,始终执行任何时间。除了在主线程上,你绝不能永远不会永远不会触摸界面。
Always do that any time you touch the interface from code that was called on a background thread. You must never never never never touch the interface except on the main thread.
这篇关于此应用程序正在从后台线程修改autolayout引擎 - ios9的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!