Using Alamofire I need to make a set of server requests and wait until all of them either succeed or fail and collect the results into a result object.
Is there a proper/provided way to do this in Alamofire?
I could put the each individual result into a queue and wait until the count is = total # of requests (obviously in a separate thread) but that seems a little clunky.
To summarize this question and answer How to tell if blocks in loop all have completed executing?, you want to create a dispatch group, enter the group as you start each operation, exit the group as you complete each operation, and finally use display_group_notify
to execute a block once the group has completed:
let group = dispatch_group_create()
foreach operation {
dispatch_group_enter(group)
startOperation(..., completion:{
dispatch_group_leave(group)
})
}
dispatch_group_notify(group, dispatch_get_main_queue()) {
// code to run when all operations complete
}