I can't change my view until my request and to find and fill my object.
I tried to put my code assync with GCD. That don't work
override func viewDidLoad() {
getHeartStroke()
NotificationCenter.default.addObserver(forName:NSNotification.Name("HeartStrokeNotification"), object: nil, queue: nil, using: notificationFinish)
}
func getHeartStroke() {
AF.request("http://localhost:8080/heartstroke", method: .get, headers: nil).responseJSON(completionHandler: {response in
if (response.error == nil)
{
let json = JSON(response.result.value!)
DispatchQueue.global(qos: .userInitiated).async {
guard let hearstrokeArray = try? JSONDecoder().decode([HeartStroke].self, from: json.rawData()) else{
debugPrint("An error has occurred")
return
}
NotificationCenter.default.post(name:NSNotification.Name("HeartStrokeNotification"), object: hearstrokeArray, userInfo: nil)
}
}else{
NotificationCenter.default.post(name:NSNotification.Name("HeartStrokeErrorNotification"), object: nil, userInfo: nil)
}
})
}
func notificationFinish(notification:Notification) -> Void{
if (notification.name.rawValue == "HeartStrokeNotification"){
arrayHeartstroke = notification.object as! [HeartStroke]
DispatchQueue.main.async(execute: {
self.tableView.reloadData()
})
}
With this code I stay block on my page until the end of getHeartStroke(), I expect to navigate in my app in same time of the fetch.