我正在构建一个将列出所有用户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
我正在使用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请求
发布评论