多个Alamofire请求 [英] multiple Alamofire requests
问题描述
我正在构建一个将列出所有用户instagram关注者的应用。 Instagram API仅以JSON数组的形式返回大约50个关注者,并返回到下50个关注者的链接,依此类推,直到发送所有关注者为止。这种方法称为分页。
I'm building an app that will list all of a user instagram followers. Instagram API returns only around 50 followers as JSON array and a link to the next 50 and so on till all the followers are sent. This method is called pagination.
我要执行的操作是以下
What I'm trying to do is the following
- 用主API URl
- 将所有已发送的数据附加到JSON数组
- 重新发送请求,但将URL设置为分页URL(将列出接下来的50个关注者)
- 将新发送的关注者追加到JSON数组
- 继续这样做,直到接下来的50个网址为空(没有更多关注者)
- Start a request with the a main API URl
- append all the sent data to a JSON array
- Re-Send a request but with URL set as the pagination URL( that will list the next 50 followers)
- append the new sent followers to the JSON array
- Keep on doing so until the url for the next 50 is empty ( no more followers)
我正在使用Swift 1.2,Xcode 6.4,Alamofire最新版本,SwiftyJSON最新版本
I'm using Swift 1.2 , Xcode 6.4, Alamofire lastest release, SwiftyJSON latest Release
我使用的代码
Here is the code i used
do {
Alamofire.request(.GET, dataURL).responseJSON { (request, response, json, error) in
print("Request Started")
if json == nil || error != nil {
let alertView = UIAlertController(title: "Error", message: "No Data found. Check your internet connection", preferredStyle: .Alert)
alertView.addAction(UIAlertAction(title: "Dismiss", style: .Default, handler: nil))
self.presentViewController(alertView, animated: true, completion: nil)
return
}else{
var jsonObject = JSON(json!)
var dataOfNextURl = jsonObject["pagination"]["next_url"].string!
print(" Data for next url :\(dataOfNextURl)")
dataURL = dataOfNextURl
}
}
}while(dataURL != "")
在控制台中,我得到无数的 Request Started打印(无限循环,没有启动请求并等待响应),但是我没有得到打印结果(下一个网址的数据:(dataOfNextURl))
已执行。我猜想Alamofire旨在在不同的线程上工作以降低复杂性,但如果我单独发出一个请求,而没有 while
循环,它会按预期工作(等待响应之前
那么我该怎么做呢?让alamofire在循环之前等待每个响应
谢谢
In the console I'm getting endless "Request Started " printed ( infinite loop, not initiating the request and waiting for the response), but I'm not getting an of the print(" Data for next url :(dataOfNextURl)")
executed. I'm guessing that Alamofire is designed to work on a different thread for reducing complexity but if i issue a request alone without the while
loop it works as supposed( waits for response before executing later code.
So how can I do this? make alamofire wait for every response before iterating the loop. Thanks
推荐答案
NSUserDefaults为此具有 registerDefaults:
方法。请使用它(在 application:didFinishLaunchingWithOptions中:
)。
NSUserDefaults has a registerDefaults:
method for this purpose. Use it (in application:didFinishLaunchingWithOptions:
).
这篇关于多个Alamofire请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!