This application modifies the autorun mechanism from the 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() 

I do not understand why I am getting this error. I have to put the reloadData() method inside the task , because I need to wait until I get the data. How else can I reload a table view without getting this error?

+1
source share
1 answer

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

You go to the main thread to call reloadData , for example:

 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 this when you touch an interface from code that was called in the background thread. You should never ever touch an interface other than the main thread.

+1
source

Source: https://habr.com/ru/post/1243919/


All Articles