StevenUpForever
StevenUpForever

Reputation: 66

Concurrent execution and NSOperationQueue management

I have two questions about multi-threads in iOS

  1. if I want to simulate 100 concurrent API call, which means these 100 API calls start at the same time, how should I do it?

Like if something like this

for i in 0..<100 {
    //Start API call
}

or add 100 operations in one operationQueue and set max concurrent over 100, start the operations in the queue

all these should means start the API call one by one, so how to start them at the same time? just like add 100 operations and start together

  1. How to add and monitor NSURLSessionDataTask objects in operationQueue? Like use waitUntilAllOperationsDone() method for multiple NSURLSessioNDataTask Objects.

I'm using something like

dispatch_group_enter(group)
session.dataTaskWithCompletion({
   dispatch_group_leave(group)
})
dispatch_group_notify()

I'm wondering if this could be implemented in NSOperationQeue, it seems every thread created by NSURLSessionDataTask is randomly created by system so how to monitor it in NSOperationQueue?

Upvotes: 0

Views: 249

Answers (1)

Rob
Rob

Reputation: 438467

Whether you start these 100 tasks by just calling some asynchronous method or using an operation queue, the effect is similar, that they won't technically all start at exactly the same time, but they should (with a few caveats) start closely enough to each other such that, assuming the asynchronous API call doesn't return instantaneously, you'll certainly have them running at the same time.

Note, if the API is using NSURLSession, you may have to adjust the httpMaximumConnectionsPerHost of your NSURLSessionConfiguration. Usually it's constrained to some reasonable value, and if you want 100 concurrent tasks (more than you'd ever want to use in a production environment), you may have to adjust this setting.

You ask a few questions about operation queues. There's little benefit, IMHO, in introducing operation queues into this discussion. In fact, one of the main use cases for operation queues is to achieve the precise opposite, when you don't want them all running concurrently, but rather want to constrain the concurrency to something reasonable (e.g. 4 at a time). So operation queues might be part of your eventual production solution, but don't introduce it solely for the sake of trying to run a lot of requests at the same time.

Also note, when trying to run a bunch of asynchronous tasks on operation queue, you can't just addOperationWithBlock or add an NSBlockOperation. You have to make an asynchronous custom subclass of NSOperation that sets the isAsynchronous flag and does the necessary KVO of isFinished and isExecuting. It's not hard, but it's enough work that it's not something I'd introduce without a compelling need.

Regarding dispatch group "notify" vs operation queue, yes, you can achieve something similar using operation queues. You can just create some "completion" operation which is dependent upon all of the other operations finishing and add that to some queue after all of the other operations have been queued.

Upvotes: 0

Related Questions