I want to setup a serialized task queue using NSOperationQueue but I'm a little confused by the terminology discussed in the documentation.
In the context of an NSOperation object, the terms concurrent and non-concurrent do not necessarily refer to the side-by-side execution of threads. Instead, a non-concurrent operation is one that executes using the environment that is provided for it while a concurrent operation is responsible for setting up its own execution environment.
What does it mean to setup "own execution environment'?
My NSOperation derived tasks need to execute serially in the order they were added to the queue.
So I thought that this implies a 'non-concurrent' operation so I'd implement 'main' for the work that needs to be completed and also return NO for 'isConcurrent'. In addition, NSOperationQueue's 'setMaxConcurrentOperationCount' would be set to 1.
The reason I am not setting up NSOperation dependency between these tasks is because the order they're inserted into the queue is the order they should be completed.
Are these assumptions correct?
NSOperationQueue
always executes operations concurrently, while taking dependencies into account.
A "non-concurrent" operation requires a separate thread in order to execute concurrently. NSOperationQueue
is responsible for providing this thread. In other words, a non-concurrent operation depends on NSOperationQueue
to make it a concurrent operation.
A "concurrent" operation is concurrent on its own; it doesn't need NSOperationQueue
to create a thread for it. An example would be an operation that uses asynchronous file IO.
If you want two or more operations to execute serially you need to use dependencies.
If you want an operation to block the main thread then don't use NSOperationQueue
; just run each operation one after the other on the main thread.
To manually set maximum of concurrent operations, use method on operationQueue setMaxConcurrentOperationCount:
Do you really need to subclass NSOperation
? Why not just use NSInvocationOperation
and its addDependency:
method?
See my answer in this SO question.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With