I am using swift 3.0 running under iOS 10.0 and I want to craft some code that fires when a batch condition is met.
for i in 0 ..< rex {
async code, disappears and does it stuff
}
Imagine the async code is a collection of URL requests, that basically background as soon as I loop thru them. Now how can I fire off more code when "rex" requests have completed?
I thought of setting up a timer to watch and check every second, but its surely not a good solution.
I thought kicking off another thread to simply watch the data being collected, and fire when its quota is full, but well that's worse then the timer really.
I am thinking to include a test at the end of each URL request to see if it was the last that completed and than uses the NotificationCenter, but is this the optimal solution?
What you're looking for is
NSOperationQueue
(orOperationQueue
in Swift 3). Here's a Swift tutorial (might be a bit out of date). Here's Apple's documentation on it -- in Swift 3 they drop all the NS prefixes, so it'sOperationQueue
/Operation
.Basically you should add each of your URL tasks as an
Operation
to anOperationQueue
, and have a "done"Operation
with each of your URL tasks as a dependency, and add it to the queue. Then as soon as all your URL tasks are done, it will call your done operation, which you can set up to do whatever you want.You will probably need to subclass
Operation
so you can update theisExecuting
andisFinished
properties properly. This question may be of some help here.While
OperationQueue
(akaNSOperationQueue
) is a good choice in many cases, it's not suitable for your use case. The problem is that URL requests are called asynchronously. YourNSOperation
will finish before you get a response from the webservice.Use
DispatchGroup
insteadSo I'm pretty sure what you want can be found here. Basically you want to use GCD and have a completion closure. It's one line of code, which always makes me giggle. A longer post on the topic is here.