-1

I'm building my app with Swift and I'm using Alamofire for my web requests. I need to perform 4 requests at the same time and then run a function only when information is downloaded. How can I do that? Thank you!

rmaddy
  • 314,917
  • 42
  • 532
  • 579
Dmitrij Rogov
  • 183
  • 1
  • 2
  • 9
  • The 4 requests need to be done at the same time? Or one depend on the other and need to requested sequentially? – Henrique da Costa Apr 06 '16 at 14:19
  • at the same time. I know how to do this sequentially, thank you – Dmitrij Rogov Apr 06 '16 at 14:25
  • This is a little confusing. Do you actually need these operations to happens at the exact same instant or can they be delayed by milliseconds. Same instant, I have no clue. Milliseconds already happens with async functions. – Dan Beaulieu Apr 06 '16 at 14:59

2 Answers2

1

You can use dispatch_group_async and dispatch_group_notify. Create group of requests with dispatch_group_async and dispatch_group_notify will notify you once all the requests are complete.

Shobhit C
  • 828
  • 10
  • 15
0

An easy way to do this would be to have a variable you would lock (or not if it's an atomic variable) you would increment after each request is executed. If it reaches 4 once you increment it, execute your function.

Max Dum
  • 181
  • 1
  • 1
  • 8
  • grand central dispatch has great facilities for these types of operations as well – Dan Beaulieu Apr 06 '16 at 14:57
  • I managed this by myself and I'm using `NSTimer()` to check if variable is equal to 4. Can I do this without timer? Thank you! – Dmitrij Rogov Apr 06 '16 at 15:06
  • Actually I think I found exactly what you need (and what Dan is speaking of). Check this out : http://stackoverflow.com/a/11909880/4021216 – Max Dum Apr 07 '16 at 18:12