在 swift 应用程序中构造 Alamofire 请求的体面方式
A decent way to structure Alamofire requests in swift app
我是一个 iOS 新手,有一个方法可以使用 Alamofire 从服务器加载一些 json 并解析 JSON。我的代码看起来有点像下面的代码,每 5 秒重试一次。
func loadData() {
let end_point = "likes/" + String(UserInfo.sharedData.userId)
let url = MyConfig.sharedData.url + end_point
Alamofire.request(.GET, url).responseJSON{ (request, response, json, error) in
if (error == nil) {
println(request, response, error)
var products = ParseProduct(json)
for product in products {
self.likes.append(product)
}
self.collectionView.reloadData()
} else {
println("failed will try again in 5 seconds")
let delayInSeconds = 5.0
let delay = dispatch_time(DISPATCH_TIME_NOW, Int64(delayInSeconds * Double(NSEC_PER_SEC)))
dispatch_after(delay, dispatch_get_main_queue()) {
self.loadData()
}
}
}
}
为我的每个 Alamofire 请求重复此重试似乎很乏味。为多个 URL 的多个请求构建此代码的好方法是什么。
将重试逻辑移到它自己的方法中。将 URL 作为参数传递,然后提供一个 completionHandler
,调用者将在其中提供自己的自定义逻辑:
func performAndRetryRequestWithURL(url: String, completionHandler:(AnyObject?) -> Void) {
Alamofire.request(.GET, url).responseJSON{ (request, response, json, error) in
if error == nil {
completionHandler(json)
} else {
let delay = dispatch_time(DISPATCH_TIME_NOW, Int64(5 * Double(NSEC_PER_SEC)))
dispatch_after(delay, dispatch_get_main_queue()) {
self.performAndRetryRequestWithURL(url, completionHandler: completionHandler)
}
}
}
}
然后loadData
可以使用那个函数
func loadData() {
let endPoint = "likes/" + String(UserInfo.sharedData.userId)
let url = MyConfig.sharedData.url + endPoint
performAndRetryRequestWithURL(url) { json in
let products = ParseProduct(json)
self.likes += products
self.collectionView.reloadData()
}
}
我是一个 iOS 新手,有一个方法可以使用 Alamofire 从服务器加载一些 json 并解析 JSON。我的代码看起来有点像下面的代码,每 5 秒重试一次。
func loadData() {
let end_point = "likes/" + String(UserInfo.sharedData.userId)
let url = MyConfig.sharedData.url + end_point
Alamofire.request(.GET, url).responseJSON{ (request, response, json, error) in
if (error == nil) {
println(request, response, error)
var products = ParseProduct(json)
for product in products {
self.likes.append(product)
}
self.collectionView.reloadData()
} else {
println("failed will try again in 5 seconds")
let delayInSeconds = 5.0
let delay = dispatch_time(DISPATCH_TIME_NOW, Int64(delayInSeconds * Double(NSEC_PER_SEC)))
dispatch_after(delay, dispatch_get_main_queue()) {
self.loadData()
}
}
}
}
为我的每个 Alamofire 请求重复此重试似乎很乏味。为多个 URL 的多个请求构建此代码的好方法是什么。
将重试逻辑移到它自己的方法中。将 URL 作为参数传递,然后提供一个 completionHandler
,调用者将在其中提供自己的自定义逻辑:
func performAndRetryRequestWithURL(url: String, completionHandler:(AnyObject?) -> Void) {
Alamofire.request(.GET, url).responseJSON{ (request, response, json, error) in
if error == nil {
completionHandler(json)
} else {
let delay = dispatch_time(DISPATCH_TIME_NOW, Int64(5 * Double(NSEC_PER_SEC)))
dispatch_after(delay, dispatch_get_main_queue()) {
self.performAndRetryRequestWithURL(url, completionHandler: completionHandler)
}
}
}
}
然后loadData
可以使用那个函数
func loadData() {
let endPoint = "likes/" + String(UserInfo.sharedData.userId)
let url = MyConfig.sharedData.url + endPoint
performAndRetryRequestWithURL(url) { json in
let products = ParseProduct(json)
self.likes += products
self.collectionView.reloadData()
}
}