我目前正在开发 Swift 3 应用程序,我对该应用程序的响应时间感到非常惊讶......
通过 postman ,我的请求在 200 毫秒内响应,但在应用程序中,感觉在 roquets 和响应之间花费了超过 10 秒的时间......
以下是请求花费过多时间的示例:
class func getFriendPage(page : Int, callback : @escaping (Bool, JSON) -> Void) {
let url = NetworkManager.getUrl() + "api/v1/challenges/friends?page=" + String(page)
let params = [:] as [String : Any]?
Alamofire.request(url, method: .get, parameters: params, encoding: JSONEncoding.default, headers: NetworkManager.getHeader()).responseJSON { (response) -> Void in
if response.response != nil && (response.response?.statusCode)! < 300 {
print("Status Code : \(response.response?.statusCode)")
print("response : \(response.response)")
print("result : \(response.result)")
print("value : \(response.result.value)")
let json = JSON(response.result.value!)
callback(true, json)
} else {
callback(false, JSON.null)
}
}
}
如果有人能帮助我,那就太好了! :D 非常感谢! 如果您需要更多详细信息,我仍然在那里!
编辑:我尝试使用手机的个人热点在我的 Mac 上使用 postman :响应时间约为 1.5 秒,找不到我在哪里丢失了这 9 秒
最佳答案
好吧,我的错...如果没有参数,请不要使用 JSONEncoding.default...使用 URLEncoding.default...
这里是一个例子:
class func getFriendPage(page : Int, callback : @escaping (Bool, JSON) -> Void) {
let url = NetworkManager.getUrl() + "api/v1/challenges/friends?page=" + String(page)
Alamofire.request(url, method: .get, parameters: [:], encoding: URLEncoding.default, headers: NetworkManager.getHeader()).responseJSON { (response) -> Void in
if response.response != nil && (response.response?.statusCode)! < 300 {
print("Status Code : \(response.response?.statusCode)")
print("response : \(response.response)")
print("result : \(response.result)")
print("value : \(response.result.value)")
let json = JSON(response.result.value!)
callback(true, json)
} else {
callback(false, JSON.null)
}
}
}
关于swift - postman 和 alamofire 之间的响应时间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42949754/