I would advise you to reload the tableView in the completion window, and not in how you call
Swift 3
let's say you get data through NSURLsession
func getDataFromJson(url: String, parameter: String, completion: @escaping (_ success: [String : AnyObject]) -> Void) { //@escaping...If a closure is passed as an argument to a function and it is invoked after the function returns, the closure is @escaping. var request = URLRequest(url: URL(string: url)!) request.httpMethod = "POST" let postString = parameter request.httpBody = postString.data(using: .utf8) let task = URLSession.shared.dataTask(with: request) { Data, response, error in guard let data = Data, error == nil else { // check for fundamental networking error print("error=\(error)") return } if let httpStatus = response as? HTTPURLResponse, httpStatus.statusCode != 200 { // check for http errors print("statusCode should be 200, but is \(httpStatus.statusCode)") print(response!) return } let responseString = try! JSONSerialization.jsonObject(with: data, options: .allowFragments) as! [String : AnyObject] completion(responseString) } task.resume()
}
Since you are using Alomofire, you can change accordingly
getDataFromJson(url: "http://....", parameter: "....", completion: { response in print(response)
// You are 100% sure that you got your data, then clear your array, load newData and reload the tableView in the main thread, as you do orders = []
let info = Order(shopname: shopname, shopaddress: shopaddr, clientName: cleintName,ClientAddress: clientAddres, PerferTime: time, Cost: subtotal , date : time , Logo : logoString ,id : id) self.orders.append(info) DispatchQueue.main.async { self.tableview.reloadData() } })
Completion blocks should solve your problem