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
- 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)
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 != "")
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