Inherits from | |
Conforms to | |
Framework | /System/Library/Frameworks/Foundation.framework |
Availability | Available in Mac OS X v10.5 and later. |
Companion guide | |
Declared in | NSOperation.h |
Related sample code |
The NSOperationQueue
class manages a set of NSOperation
objects in a priority queue and regulates their execution. Operations remain in the queue until they are explicitly cancelled or finish executing. An application may create multiple operation queues, with each queue running up to its designated maximum number of operations.
A specific NSOperation
object can be in only one operation queue at a time. Operations within a single queue coordinate their execution order using both priority levels and inter-operation object dependencies. Operation objects in different queues can coordinate their execution order using dependencies, which are not queue-specific.
Inter-operation dependencies provide an absolute execution order for operations. An operation object is not considered ready to execute until all of its dependent operations have finished executing. For operations that are ready to execute, the operation queue always executes the one with the highest priority relative to the other ready operations. For details on how to set priority levels and dependencies, see NSOperation Class Reference.
You should never manually start an operation while it is sitting in an operation queue. Once added, an operation stays in its queue until it finishes executing or is cancelled.
If the isConcurrent
method of an operation returns NO
, the operation queue automatically creates a new thread for that operation before running it. If the isConcurrent
method returns YES
, the operation object must create its own thread or otherwise configure its own runtime environment as part of its execution phase.
The NSOperationQueue
class is key-value coding (KVC) and key-value observing (KVO) compliant. You can observe these properties as desired to control other parts of your application. The properties you can observe include the following:
operations
- read-only property
maxConcurrentOperationCount
- readable and writable property
For more information about key-value observing and how to attach observers to an object, see Key-Value Observing Programming Guide.
It is safe to use a single NSOperationQueue
object from multiple threads without creating additional locks to synchronize access to that object.
Adds the specified operation object to the receiver.
- (void)addOperation:(NSOperation *)operation
The operation object to be added to the queue. In memory-managed applications, this object is retained by the operation queue.
An operation object can be in at most one operation queue at a time and cannot be added if it is currently executing or finished. This method throws an NSInvalidArgumentException
exception if any of these conditions is true.
Once added, the specified operation remains in the queue until it is executed or cancelled.
– cancel
(NSOperation)– isExecuting
(NSOperation)NSOperation.h
Cancels all queued and executing operations.
- (void)cancelAllOperations
This method sends a cancel
message to all operations currently in the queue or executing. Queued operations are cancelled before they begin executing. If an operation is already executing, it is up to that operation to recognize the cancellation and stop what it is doing.
cancel
(NSOperation
)NSOperation.h
Returns a Boolean value indicating whether the receiver is scheduling queued operations for execution.
- (BOOL)isSuspended
NO
if operations are being scheduled for execution; otherwise, YES
.
NSOperation.h
Returns the maximum number of concurrent operations that the receiver can execute.
- (NSInteger)maxConcurrentOperationCount
The maximum number of concurrent operations set explicitly on the receiver using the setMaxConcurrentOperationCount:
method.
NSOperation.h
Returns a new array containing the operations currently in the queue.
- (NSArray *)operations
A new array object containing the NSOperation
objects in the order in which they were added to the queue.
NSOperation.h
Sets the maximum number of concurrent operations that the receiver can execute.
- (void)setMaxConcurrentOperationCount:(NSInteger)count
The maximum number of concurrent operations. Specify the value NSOperationQueueDefaultMaxConcurrentOperationCount
if you want the receiver to choose an appropriate value based on the number of available processors and other relevant factors.
The specified value affects only the receiver and the operations in its queue. Other operation queue objects can also execute their maximum number of operations in parallel.
Reducing the number of concurrent operations does not affect any operations that are currently executing. If you specify the value NSOperationQueueDefaultMaxConcurrentOperationCount
(which is recommended), the maximum number of operations can change dynamically based on system conditions.
NSOperation.h
Modifies the execution of pending operations
- (void)setSuspended:(BOOL)suspend
If YES
, the queue stops scheduling queued operations for execution. If NO
, the queue begins scheduling operations again.
This method suspends or restarts the execution of queued operations only. It does not have any impact on the state of currently running operations. Running operations continue to run until their natural termination or until they are explicitly cancelled.
NSOperation.h
Blocks the current thread until all of the receiver’s queued and executing operations finish executing.
- (void)waitUntilAllOperationsAreFinished
When called, this method blocks the current thread and waits for the receiver’s current and pending operations to finish executing. While the thread is blocked, the receiver continues to launch already queued operations and monitor those that are executing. During this time, the current thread cannot add operations to the queue, but other threads may. Once all of the pending operations are finished, this method returns.
NSOperation.h
Indicates the number of supported concurrent operations.
enum { NSOperationQueueDefaultMaxConcurrentOperationCount = -1 };
NSOperationQueueDefaultMaxConcurrentOperationCount
The default maximum number of operations is determined dynamically by the NSOperationQueue
object based on current system conditions.
Available in Mac OS X v10.5 and later.
Declared in NSOperation.h
.
NSOperation.h
© 2008 Apple Inc. All Rights Reserved. (Last updated: 2008-11-19)