A solution to track a batch of HTTP requests in swift 3.0

后端 未结 3 525
伪装坚强ぢ
伪装坚强ぢ 2021-01-27 13:10

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, disapp         


        
3条回答
  •  粉色の甜心
    2021-01-27 14:00

    What you're looking for is NSOperationQueue (or OperationQueue 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's OperationQueue / Operation.

    Basically you should add each of your URL tasks as an Operation to an OperationQueue, 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 the isExecuting and isFinished properties properly. This question may be of some help here.

提交回复
热议问题