【发布时间】:2017-08-24 12:45:11
【问题描述】:
我知道测试 API 的响应时间基本上是由服务器端或后端完成的,但是由于我正在为一个应用程序工作,我还需要从 iOS 端检查 api 响应时间。
我该怎么做?我读了几个链接,上面说要使用计时器开始和计时器结束,然后通过 endTime - startTime 找到响应时间,但这似乎并不方便。
我想使用 Xcode(即使有 XCTest)。
这是我的一个 API(我在 ApiManager 类的单独类中编写了所有 Web 服务使用方法):
登录VC:
//Call Webservice
let apiManager = ApiManager()
apiManager.delegate = self
apiManager.getUserInfoAPI()
ApiManager:
func getUserInfoAPI() {
//Header
let headers = [
"Accept" : "application/json",
"Content-Type" : "application/json",
]
//Call Web API using Alamofire library
AlamoFireSharedManagerInit()
Alamofire.request(HCConstants.URL, method: .post, parameters: nil, encoding: JSONEncoding.default, headers: headers).responseJSON { response in
do{
//Checking For Error
if let error = response.result.error {
//Stop AcitivityIndicator
self.hideHud()
//Call failure delegate method
//print(error)
self.delegate?.APIFailureResponse(HCConstants.EXCEPTION_MESSAGES.SERVICE_FAILURE)
return
}
//Store Response
let responseValue = try JSONSerialization.jsonObject(with: response.data!, options: JSONSerialization.ReadingOptions()) as! Dictionary<String, AnyObject>
print(responseValue)
//Save token
if let mEmail = responseValue[HCConstants.Email] as? String {
UserDefaults.standard.setValue(mEmail, forKey: HCConstants. mEmail)
}
//Stop AcitivityIndicator
self.hideHud()
//Check Success Flag
if let _ = responseValue["info"] as? String {
//Call success delegate method
self.delegate?.apiSuccessResponse(responseValue)
}
else {
//Failure message
self.delegate?.APIFailureResponse(responseValue["message"] as? String ?? HCConstants.EXCEPTION_MESSAGES.SERVICE_FAILURE)
}
} catch {print("Exception is there "}
}
}
【问题讨论】:
标签: ios performance swift3 response