Search code examples
iosnsoperationnsoperationqueue

NSOperation and NSOperationQueue with maxConcurrentOperationCount = 1


I wanted to implement a serial queue with NSOperationQueue.So I have set maxConcurrentOperationCount = 1 for my NSOperationQueue. So couple of question now

Now do I still need to set concurrent to YES in subclass NSOperation to make it serial?

If I set concurrent to YES for NSOperation, though the maxConcurrentOperationCount is 1 is it still possible that I can have 2 or more NSOperations running parallely ??


Solution

  • Based on how I read Apple's documentation, the concurrent property in NSOperation is readonly, and tells us if the operation will run asynchronously or not. If you plan to start operations manually, you need to make the your NSOperation return YES for asynchronous in order to avoid blocking the thread you are starting your operations from. The concurrent property is just used to monitor the state of the operation if you are running them manually

    If you are adding your NSOperation to an NSOperationQueue, the queue will ignore the value of the asynchronousproperty, and run operations according to the maxConcurrentOperationCount.

    So, to answer your question: If you run all your operations manually, and set asynchronous to YES, the number of operations running in parallel will depend on how big the delay is between each time you call start on your operations, and how long it will take to finish them. If you add them to a queue, your queue will run the operations one by one, as a serial queue.