等待多个 Alamofire 请求完成
Posted
技术标签:
【中文标题】等待多个 Alamofire 请求完成【英文标题】:Wait for multiple Alamofire Requests to finish 【发布时间】:2017-10-15 10:31:41 【问题描述】:我的网络类中有一个下面的函数,它执行 Alamofire 请求过程。我在我的模型类中调用这个函数,当 Alamofire 完成请求时,它调用通知模型类的委托函数。我的问题是我多次调用这个 Alamofire 函数,并且两者都应该在通知模型类之前完成。现在我用非常愚蠢的方式处理它。正如我搜索的那样,可以使用 DispatchGroup 但我无法弄清楚,如何实现它。谢谢。
模型类
@objc func refresh_fiks()
let network = Network()
network.delegate = self
self.teams = [[]]
network.getRequest(req: 1)
network.getRequest(req: 2)
请求函数:
func response()
print(response_json.count)
if(response_json.count == path)
self.delegate?.sendJson(response_json)
func getRequest(req: Int)
path = req
let rot = Router(method: .get, path: req, parameters: nil)
Alamofire.request(rot)
.response response in
print(response.request?.url! as Any)
// check for errors
guard response.error == nil else
// got an error in getting the data, need to handle it
print(response.error!)
let errorJson: JSON = [ "Error" : "Can't get the data!"]
self.response_json.append(errorJson)
self.response()
return
// make sure we got some JSON since that's what we expect
guard (response.data?.base64EncodedString()) != nil else
print("Error: \(String(describing: response.error))")
let errorJson: JSON = [ "Error" : "Can't get the data!"]
self.response_json.append(errorJson)
self.response()
return
guard response.response?.statusCode == 200 else
let errorJson: JSON = [ "Error" : "Can't get the data!"]
self.response_json.append(errorJson)
self.response()
return
let json = JSON(data: response.data!)
// get and print the title
if json != nil
self.response_json.append(json)
self.response()
else
let errorJson: JSON = [ "Error" : "Can't get the data!"]
self.response_json.append(errorJson)
self.response()
return
【问题讨论】:
【参考方案1】:我对您的代码做了一些更改:
网络类:
func response(array: [JSON])
print(array.count)
if(array.count == path)
self.delegate?.sendJson(array)
func getMultipleRequests(_ requests: [Int])
DispatchQueue.global(qos: .background).async
let group = DispatchGroup()
var array: [JSON] = []
for request in requests
group.enter()
self.getRequest(req: request, completion: (json) in
array.append(json)
group.leave()
)
group.wait()
//this line below won't be called until all entries will leave the group
self.response(array: array)
func getRequest(req: Int, completion: @escaping (_ json: JSON) -> Void)
path = req
let rot = Router(method: .get, path: req, parameters: nil)
Alamofire.request(rot)
.response response in
print(response.request?.url! as Any)
// check for errors
guard response.error == nil else
// got an error in getting the data, need to handle it
print(response.error!)
let errorJson: JSON = [ "Error" : "Can't get the data!"]
completion(errorJson)
return
// make sure we got some JSON since that's what we expect
guard (response.data?.base64EncodedString()) != nil else
print("Error: \(String(describing: response.error))")
let errorJson: JSON = [ "Error" : "Can't get the data!"]
completion(errorJson)
return
guard response.response?.statusCode == 200 else
let errorJson: JSON = [ "Error" : "Can't get the data!"]
completion(errorJson)
return
let json = JSON(data: response.data!)
// get and print the title
if json != nil
completion(json)
else
let errorJson: JSON = [ "Error" : "Can't get the data!"]
completion(errorJson)
return
所以 getRequest 函数现在有一个完成块,它将从每个请求中返回一个 json 结果,而 getMultipleRequests 函数将接收来自任何人的一堆请求 p>
这是你如何使用它
你的类,调用 refresh_fiks:
@objc func refresh_fiks()
let network = Network()
network.delegate = self
self.teams = [[]]
network.getMultipleRequests([1,2])
此外,您可能需要使用 group.notify 而不是使用 group.wait(),最好通知所有条目在指定队列中离开组,像 main:
group.notify(queue: DispatchQueue.main, execute:
print("All Done")
self.response(array: array)
)
关于 DispatchGroups 的阅读内容:
RayWenderlich
ALL ABOUT SWIFT
【讨论】:
以上是关于等待多个 Alamofire 请求完成的主要内容,如果未能解决你的问题,请参考以下文章