This application is modifying the autolayout engine from a background thread - ios9

房东的猫 提交于 2019-12-02 10:14:36

问题


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()

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?


回答1:


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

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.



来源:https://stackoverflow.com/questions/33715819/this-application-is-modifying-the-autolayout-engine-from-a-background-thread-i

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!