我刚开始通过Alamofire建立关系网,在网络方面我是新手。
我正在进行Alamofire Api调用中的响应验证,因此根据行业实践,在api请求之前检查是否更好,还是使用alamofire接收到的错误代码处理它?。
我读过在进行互联网呼叫之前检查互联网连接会导致应用程序开销
{
let manager = Alamofire.SessionManager.default
manager.session.configuration.timeoutIntervalForRequest = 60
manager.request("http://api.androidhive.info/contacts/")
.validate()
.responseJSON {
response in
switch (response.result) {
case .success:
// code
}
case .failure(let error):
if error._code == NSURLErrorTimedOut {
print("Request call timed Out !!")
self.displayText?.text = "Request call timed Out !!"
} else if error._code == -1009{
print(" You Sir are not Connected to the Internet !!")
self.displayText?.text = "You Sir are not Connected to the Internet !!"
} else if error._code == -1003 {
print(" \t bruh, \n Atleast provide a Valid URL ")
self.displayText?.text = "bruh,Atleast provide a Valid URL "
}
else {
print("Meh, Some Kind of error with errorCode: \(error._code) !!")
self.displayText?.text = "Meh, Some Kind of error with errorCode: \(error._code) !!"
}
}
}发布于 2017-07-03 09:57:07
不,你不应该在打电话之前检查互联网的连接。苹果公司特别建议不要这样做。只有在请求失败后才能使用可达性,以便在连接恢复联机时自动重试请求。
发布于 2017-06-21 08:01:07
If Internet is available {
//Make rest calls.
}else{
// Return cached response.
}这取决于你,首先检查互联网节省了计算资源和请求创建和api调用。
还提供了返回缓存数据(如果有)的选项。
发布于 2017-06-21 08:06:19
实际上,这取决于您和您的产品的具体要求。这是一个很好的做法,然而,当用户试图访问互联网时,不显示任何加载指示符,让他等待,这是一个很好的做法。我的建议是在打任何电话之前不要检查网络的可达性,而是通过Alamofire的NetworkReachabilityManager类听网络可达性的变化,并对你的用户界面进行适当的更改(例如,在互联网消失时禁用网络相关按钮等)。
https://stackoverflow.com/questions/44669464
复制相似问题