在重新加载表视图之前,我想要完成这两个请求(表视图取决于它们)。
我尝试过如下,但它不起作用。
请求1完成,然后在请求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){
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()
}
答案 0 :(得分:0)
尝试使用DispatchSemaphore
func serverRequestAuth(_ requestName: String){
let requestNameEscaped = requestName.addingPercentEncoding(withAllowedCharacters: CharacterSet.urlQueryAllowed)!
let semaphore = DispatchSemaphore(value: 0);
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)
semaphore.signal();
}
}
}
task.resume()
semaphore.wait(timeout: .distantFuture)
}
答案 1 :(得分:0)
试试这个:
function moveOperationalToTop() {
var specialId = "divOperationalDetails";
var $main = $("#divMain");
var $divs = $main.find('div');
$main.empty();
$divs.each(function() {
if (this.id == specialId) $main.append($(this));
});
$divs.each(function() {
if (this.id != specialId) $main.append($(this));
});
}
你可以用:
来调用它#divOperationalDetails {
color: red
}
答案 2 :(得分:0)
我曾经遇到过类似的问题。为了提供一个简单的解决方案,我编写了这个小型库:
https://github.com/prine/ROConcurrency
您可以在“任务”中对请求进行分组,并等待所有请求完成,然后重新加载表格。
var taskRequest1 = TaskComplex { (finished) -> () in
self.serverRequestAuth("/request1", finished)
}
var taskRequest2 = TaskComplex { (finished) -> () in
self.serverRequestAuth("/request2", finished)
}
var reloadTable = TaskComplex { (finished) -> () in
DispatchQueue.main.async{
// 3. reload the table view
self.tableView.reloadData()
finished()
}
}
var barrierComplex = BarrierComplex(tasks: [taskRequest1, taskRequest2], afterTask: reloadTable)
barrierComplex.startTasks()
您的serverRequestAuth方法需要告诉它何时异步完成,因此您需要添加完成的回调:
func serverRequestAuth(_ requestName: String, _ finished:(Bool) -> ()) {
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)
// Here you call finished to tell the Barrier that its finished
finished()
}
}
}
task.resume()
}