I am a beginner in swift and I have come across a case where I need to run AlamoFire GET request in a loop. But As far as i know , the Alamofire Get request is an Asynchronous call and calling it in loop will create a number of threads.
The requirement is :
- I have an array of URLs
The array has to be traversed in a loop
URL on each index has to be called through AlamoFire GET request
The received data through the request will be added to an array of data
After the last data is saved in the array, a CollectionView.reload call should be called
Pseudo code is as follows:
for bookmarkURL in bookmarks
Alamofire.request(.GET, bookmarkURL ).responseJSON
{ response in switch response.result {
case .Success(let JSON):
x[i] = JSON as! [String : AnyObject] // saving data
case .Failure(let error):
print("the error for \(self.bookmarkURL) is \(error) ")
}
i++
if i == bookmarks.count{
collectionView.reload()
break}
}
can anyone tell me how should I do it in sequence?