我正在为我的应用程序进行性能测试,在通过仪器进行调试后,我遇到了请求问题。我为每个人使用DispatchQueue
,并通过DispatchGroup
对其进行分组。有人可以看看并指出我在哪里犯错误吗?
这是responseHandler
:
func responseHandler(dataResponse: DataResponse<Data>, completion: @escaping (Data, NetworkCode) -> Void) {
guard (dataResponse.response?.statusCode) != nil else { completion(Data(), .error); return }
guard Connectivity.Instance.isReachability else { completion(Data(), .noInternetConnection); return }
// Response must be different than 401 - 401 code means we fucked up request.
if dataResponse.response?.statusCode == 401 {
// TODO: Handle message from server.
completion(Data(), .error)
return
}
// Return data from resonse if everything went ok ( status code 200 )
guard let data = dataResponse.data else { completion(Data(), .error); return }
completion(data, .success)
}
我基本上检查了请求状态代码和互联网连接。
队列:final let queue = DispatchQueue(label: "com.cnoon.response-queue", qos: .userInitiated, attributes: .concurrent)
电话:
//创建调度组。
let group = DispatchGroup()
// Get first data.
group.enter()
let queryParams = self.buildQueryParams(requestType: .getFirstData(token))
self.firstRequestWith(queryParams, completion: {
// Here is a createRequest method call - inside firstRequestWith
print("1 request")
group.leave()
})
// Get second data.
group.enter()
let queryParams = self.buildQueryParams(requestType: .getSecondData(token))
self.secondRequestWith(queryParams, completion: {
// Here is a createRequest method call - inside secondRequestWith
print("1 request")
group.leave()
})
group.wait()
group.notify(queue: .main, work: DispatchWorkItem(block: {
print("Main")
}))
buildQueryParams
方法返回[String: AnyObject]
类型的字典。
我是否理解多线程Alamofire请求?
提前致谢!