I have these 2 requests that I want completed before reloading the table view(table view depends on both of them).
I tried like below, but it does not work.
Request 1 gets completed, then the table view reloads before everything being done in request 2.
DispatchQueue.global(qos: DispatchQoS.QoSClass.default).async {
//1. request 1:
self.serverRequestAuth("/request1")
//2. request 2:
self.serverRequestAuth("/request2")
DispatchQueue.main.async{
//3. reload the table view
self.tableView.reloadData()
}
}
How I should I proceed?
Mention: serverRequestAuth contains the server request, the parsing of the json, the parsing of the dictionary inside it.
The method serverRequestAuth:
func serverRequestAuth(_ requestName: String){
let requestNameEscaped = requestName.addingPercentEncoding(withAllowedCharacters: CharacterSet.urlQueryAllowed)!
var request = NSMutableURLRequest()
request = NSMutableURLRequest(url: URL(string: "\(self.view.getServerPath())\(requestNameEscaped)")!, cachePolicy: NSURLRequest.CachePolicy.reloadIgnoringLocalCacheData, timeoutInterval: 5)
request.httpMethod = "GET"
request.setValue(self.view.getAuth(), forHTTPHeaderField: "Authorization")
request.setValue("application/json", forHTTPHeaderField: "Content-Type")
let task = URLSession.shared.dataTask(with: request as URLRequest) {data, response, err in
if data != nil {
if let dictionary = self.parseJSONData(data!) {
self.parseDictionary(dictionary, typeOfRequest: requestName)
}
}
}
task.resume()
}