Class: NSOperationQueue
Overview
The NSOperationQueue class regulates the execution of a set of NSOperation objects. After being added to a queue, an operation remains in that queue until it is explicitly canceled or finishes executing its task. Operations within the queue (but not yet executing) are themselves organized according to priority levels and inter-operation object dependencies and are executed accordingly. An application may create multiple operation queues and submit operations to any of them.
Class Method Summary (collapse)
-
+ currentQueue
Returns the operation queue that launched the current operation.
-
+ mainQueue
Returns the operation queue associated with the main thread.
Instance Method Summary (collapse)
-
- addOperation:
Adds the specified operation object to the receiver.
-
- addOperations:waitUntilFinished:
Adds the specified array of operations to the queue.
-
- addOperationWithBlock:
Wraps the specified block in an operation object and adds it to the receiver.
-
- cancelAllOperations
Cancels all queued and executing operations.
-
- isSuspended
Returns a Boolean value indicating whether the receiver is scheduling queued operations for execution.
-
- maxConcurrentOperationCount
Returns the maximum number of concurrent operations that the receiver can execute.
-
- name
Returns the name of the receiver.
-
- operationCount
Returns the number of operations currently in the queue.
-
- operations
Returns a new array containing the operations currently in the queue.
-
- setMaxConcurrentOperationCount:
Sets the maximum number of concurrent operations that the receiver can execute.
-
- setName:
Assigns the specified name to the receiver.
-
- setSuspended:
Modifies the execution of pending operations This method suspends or resumes the execution of operations.
-
- waitUntilAllOperationsAreFinished
Blocks the current thread until all of the receiver’s queued and executing operations finish executing.
Methods inherited from NSObject
#!, #!=, #!~, #, #==, #===, #=~, #Rational, #__callee__, #__method__, #__send__, #__type__, `, alloc, allocWithZone:, #autoContentAccessingProxy, autoload, autoload?, autorelease_pool, #awakeAfterUsingCoder:, binding, block_given?, caller, cancelPreviousPerformRequestsWithTarget:, cancelPreviousPerformRequestsWithTarget:selector:object:, catch, class, classFallbacksForKeyedArchiver, #classForCoder, #classForKeyedArchiver, classForKeyedUnarchiver, #clone, conformsToProtocol:, #copy, copyWithZone:, #dealloc, #define_singleton_method, description, display, #doesNotRecognizeSelector:, #dup, #enum_for, #eql?, #equal?, #extend, fail, #finalize, format, #forwardInvocation:, #forwardingTargetForSelector:, framework, #freeze, #frozen?, getpass, gets, global_variables, #init, initialize, #initialize_clone, #initialize_copy, #initialize_dup, #inspect, instanceMethodForSelector:, instanceMethodSignatureForSelector:, #instance_eval, #instance_exec, #instance_of?, #instance_variable_defined?, #instance_variable_get, #instance_variable_set, #instance_variables, instancesRespondToSelector:, isSubclassOfClass:, #is_a?, iterator?, #kind_of?, lambda, load, load_bridge_support_file, load_plist, local_variables, loop, #method, #methodForSelector:, #methodSignatureForSelector:, #methods, #mutableCopy, mutableCopyWithZone:, new, #nil?, open, p, #performSelector:onThread:withObject:waitUntilDone:, #performSelector:onThread:withObject:waitUntilDone:modes:, #performSelector:withObject:afterDelay:, #performSelector:withObject:afterDelay:inModes:, #performSelectorInBackground:withObject:, #performSelectorOnMainThread:withObject:waitUntilDone:, #performSelectorOnMainThread:withObject:waitUntilDone:modes:, print, printf, #private_methods, proc, #protected_methods, #public_method, #public_methods, #public_send, putc, puts, raise, rand, readline, readlines, #replacementObjectForCoder:, #replacementObjectForKeyedArchiver:, require, resolveClassMethod:, resolveInstanceMethod:, #respond_to?, #respond_to_missing?, select, #send, setVersion:, #singleton_methods, sprintf, srand, superclass, #taint, #tainted?, #tap, test, throw, #to_plist, #to_s, trace_var, trap, #trust, #untaint, untrace_var, #untrust, #untrusted?, version
Constructor Details
This class inherits a constructor from NSObject
Dynamic Method Handling
This class handles dynamic methods through the method_missing method in the class NSObject
Class Method Details
+ (Object) currentQueue
Returns the operation queue that launched the current operation. You can use this method from within a running operation object to get a reference to the operation queue that started it. Calling this method from outside the context of a running operation typically results in nil being returned.
+ (Object) mainQueue
Returns the operation queue associated with the main thread. The returned queue executes operations on the main thread. The main thread’s run loop controls the execution times of these operations.
Instance Method Details
- (Object) addOperation(operation)
Adds the specified operation object to the receiver. Once added, the specified operation remains in the queue until it finishes executing.An operation object can be in at most one operation queue at a time and this method throws an NSInvalidArgumentException exception if the operation is already in another queue. Similarly, this method throws an NSInvalidArgumentException exception if the operation is currently executing or has already finished executing.
- (Object) addOperations(ops, waitUntilFinished:wait)
Adds the specified array of operations to the 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 those error conditions are true for any of the operations in the ops parameter. Once added, the specified operation remains in the queue until its isFinished method returns YES.
- (Object) addOperationWithBlock(block)
Wraps the specified block in an operation object and adds it to the receiver. This method adds a single block to the receiver by first wrapping it in an operation object. You should not attempt to get a reference to the newly created operation object or divine its type information. Once added, the specified operation remains in the queue until it its isFinished method returns YES.
- (Object) cancelAllOperations
Cancels all queued and executing operations. This method sends a cancel message to all operations currently in the queue. 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.
- (Boolean) isSuspended
Returns a Boolean value indicating whether the receiver is scheduling queued operations for execution. If you want to know when the queue’s suspended state changes, configure a KVO observer to observe the suspended key path of the operation queue.
- (Integer) maxConcurrentOperationCount
Returns the maximum number of concurrent operations that the receiver can execute.
- (String) name
Returns the name of the receiver. The default value of this string is
“NSOperationQueue
- (Integer) operationCount
Returns the number of operations currently in the queue. The value returned by this method reflects the instantaneous number of objects in the queue and changes as operations are completed. As a result, by the time you use the returned value, the actual number of operations may be different. You should therefore use this value only for approximate guidance and should not rely on it for object enumerations or other precise calculations.
- (Array) operations
Returns a new array containing the operations currently in the queue. You can use this method to access the operations queued at any given moment. Operations remain queued until they finish their task. Therefore, the returned array may contain operations that are either executing or waiting to be executed. The list may also contain operations that were executing when the array was initially created but have subsequently finished.
- (Object) setMaxConcurrentOperationCount(count)
Sets the maximum number of concurrent operations that the receiver can execute. 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.
- (Object) setName(newName)
Assigns the specified name to the receiver. Names provide a way for you to identify your operation queues at run time. Tools may also use this name to provide additional context during debugging or analysis of your code.
- (Object) setSuspended(suspend)
Modifies the execution of pending operations This method suspends or resumes the execution of operations. Suspending a queue prevents that queue from starting additional operations. In other words, operations that are in the queue (or added to the queue later) and are not yet executing are prevented from starting until the queue is resumed. Suspending a queue does not stop operations that are already running.Operations are removed from the queue only when they finish executing. However, in order to finish executing, an operation must first be started. Because a suspended queue does not start any new operations, it does not remove any operations (including cancelled operations) that are currently queued and not executing.
- (Object) waitUntilAllOperationsAreFinished
Blocks the current thread until all of the receiver’s queued and executing operations finish executing. When called, this method blocks the current thread and waits for the receiver’s current and queued operations to finish executing. While the current 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. If there are no operations in the queue, this method returns immediately.