I am trying to reload table data from a NSURLSession using the following function in Swift, but can't get it to reload into the table. All I want to do is update table with JSON data. Can get it to print on console but not update table.
func get_data_from_url(url:String)
{
let urlPath = url
guard let endpoint = NSURL(string: urlPath) else { print("Error creating endpoint");retu }
let request = NSMutableURLRequest(URL:endpoint)
NSURLSession.sharedSession().dataTaskWithRequest(request) { (data, response, error) -> Void in
do {
guard let dat = data else { throw JSONError.NoData }
guard let json = try NSJSONSerialization.JSONObjectWithData(dat, options: []) as? Dictionary<String, Array<Dictionary<String,String>>> else { throw JSONError.ConversionFailed }
self.myDictionary = json
let newItem = Video(name: "Bob", imageURL: "")
ItemModel.sharedInstance.categories[0].videos.append(newItem)
print ("New item: ",ItemModel.sharedInstance.categories[0].videos)
// ALL okay above the new item is displayed in the console.
// Also can add static item below resume() and this is successfully loaded onto table. Items within the NSURLSession function are not updated.
// I would expect reloadData() to work here but it is not.
self.tableView.reloadData()
} catch let error as JSONError {
print(error.rawValue)
} catch {
print(error)
}
}.resume()
//This works outside NSURLSession function.
let newItem = Video(name: "Jim", imageURL: "")
ItemModel.sharedInstance.categories[0].videos.append(newItem)
Any help our guidance would be greatly appreciated.
