테이블보기를 다시로드하기 전에이 두 가지 요청을 완료해야합니다 (테이블보기는 두 가지 모두에 따라 결정됩니다).테이블보기를 다시로드하기 전에 두 개의 서버 요청이 완료되었습니다.
아래처럼 시도했지만 작동하지 않습니다.
요청 하나는 모든 것을하기 전에 테이블 뷰를 다시로드가 내가 진행하는 방법을 요청 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()
}
}
에서 수행되고, 완료됩니다?
언급 : serverRequestAuth에는 서버 요청, json의 구문 분석, 그 안에있는 사전 구문 분석이 포함되어 있습니다.
방법의 serverRequestAuth :
func serverRequestAuth(_ requestName: String, control:@escaping (_ check : Bool)-> Void){
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)
control(true)
}else{
control(false)
}
}
}
task.resume()
}
그리고 당신은 그것을 호출 할 수 있습니다 :
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()
}
나는 모든 대답을 시도를 그 "semaphore.wait (timeout : .distantFuture)"는 나에게 경고를 주었다 : "wait (timeout :)에 대한 호출의 결과는 사용되지 않는다."그리고 이렇게 변경했다 : "_ = semaphore.wait (timeout :. 먼 미래)" – asheyla