GCD is a thread control tool written in Swift.
-
Fetch
Fetch Data from the Internet and display it by order
- Group
- Semaphore
- DeadLock
- Deadlock Prevention
Declare the variable - dispatchGroup and implement dispatchGroup enter and leave in completion handler, to make sure all data will settle down before reloading it.
let dispatchGroup = DispatchGroup()
dispatchGroup.enter()
let dataTask = URLSession.shared.dataTask(with: urlRequest) { (data, response, error) in
......
guard let response = response as? HTTPURLResponse else {
......
self.dispatchGroup.leave()
}
It will notify us once all the process finished.
self.dispatchGroup.notify(queue: .main){
self.onlineData.append(result)
self.tableView.reloadData()
}