NSOperationQueue Class Reference
NSOperationQueue 類參考
轉載請註明出處,謝謝。
原文網址:http://developer.apple.com/documentation/Cocoa/Reference/NSOperationQueue_class/Reference/Reference.html。(Last updated: 2012-01-09)
本文網址:http://my.oschina.net/KSHime37。(2012-09-15)
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.
隊列物件(NSOperationQueue)是用於管理工做物件(NSOperation)執行的管理器。當工做被加入隊列後,會佇列在隊列中直到被取消或者完成工做。在隊列中還沒有執行的工做會按照其優先順序及從屬關係自行組織順序執行。一個程式中能夠創建多個隊列來使用。
Inter-operation dependencies provide an absolute execution order for operations, even if those operations are located in different operation queues. 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.
工做的從屬關係決定了工做之間的執行順序,甚至工做在不一樣隊列中也是如此。有從屬關係的工做在其依賴的工做完成前是不會執行的。管理器會從隊列中選取相對於其餘工做優先順序最高的來執行。設定優先順序和從屬關係的細節,請見NSOperation Class Reference。
You cannot directly remove an operation from a queue after it has been added. An operation remains in its queue until it reports that it is finished with its task. Finishing its task does not necessarily mean that the operation performed that task to completion. An operation can also be canceled. Canceling an operation object leaves the object in the queue but notifies the object that it should abort its task as quickly as possible. For currently executing operations, this means that the operation object’s work code must check the cancellation state, stop what it is doing, and mark itself as finished. For operations that are queued but not yet executing, the queue must still call the operation object’s start method so that it can processes the cancellation event and mark itself as finished.
加入隊列後的工做,是不能直接從隊列中移除的。工做會佇列在隊列中直到回報已完成才移除。已完成並不必定表明工做已經完成執行了,也能夠是表明已經被取消了。取消工做時,會向該工做物件發送消息,使之盡快的停止工做。對於執行中的工做來說,就是表明了其必需檢查當前的注銷狀態,停止目前的工做,並將自身標記為已完成。而對於還沒有執行的工做來說,管理器會呼叫該工做物件的-(void)start方法,使其處理自身的注銷事件,並標記自身為已完成。
Note: In OS X v10.6 and later, canceling an operation causes the operation to ignore any dependencies it may have. This behavior makes it possible for the queue to execute the operation’s start method as soon as possible. The start method, in turn, moves the operation to the finished state so that it can be removed from the queue. In OS X v10.5, a canceled operation does not ignore its dependencies, meaning that those dependencies must complete normally before the canceled operation can run and be removed from the queue.
備註:在OS X v10.6及其以後的版本中,取消工做會使其忽略所擁有的從屬關係,這樣能夠盡快的讓其執行-(void)start方法,使其自身的狀態改變為已完成,從而使管理器移除之。而在OS X v10.5的版本中,取消工做並不會忽略從屬關係,這表明依賴其的工做會先執行完,而後才會使之移除。
Operation queues usually provide the threads used to run their operations. In OS X v10.6 and later, operation queues use the libdispatch library (also known as Grand Central Dispatch) to initiate the execution of their operations. As a result, operations are always executed on a separate thread, regardless of whether they are designated as concurrent or non-concurrent operations. In OS X v10.5, however, operations are executed on separate threads only if their isConcurrent method returns NO. If that method returns YES, the operation object is expected to create its own thread (or start some asynchronous operation); the queue does not provide a thread for it.
管理器會提供執行隊列中工做的線程。在OS X v10.6及其以後的版本中,管理器會使用libdispatch庫(也就是GCD)來創建工做的執行。所以工做會在不一樣的線程中執行,無論其是標示為並行或非並行的工做。在OS X v10.5的版本中,只有在其-(BOOL)isConcurrent方法傳回NO的時候才會在不一樣的線程中執行,若是是傳回YES,工做物件將會創建其自身的線程(或者執行異步的工做),管理器不會為其提供線程。
Note: In iOS 4 and later, operation queues use Grand Central Dispatch to execute operations. Prior to iOS 4, they create separate threads for non-concurrent operations and launch concurrent operations from the current thread. For a discussion of the difference between concurrent and non-concurrent operations and how they are executed, see NSOperation Class Reference.
備註:在iOS 4及其以後的版本中,管理器會使用GCD執行工做。在iOS 4以前的版本中,管理器會為非並行的工做創建不一樣的線程,並行的工做則向當前線程發送。討論並行與非並行的差異以及其如何執行,請見NSOperation Class Reference
For more information about using operation queues, see Concurrency Programming Guide.
更多有關使用隊列的資訊,請見Concurrency Programming Guide。
KVO-Compliant Properties
鍵值觀察的屬性
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:
管理器(NSOperationQueue)遵循鍵值編程(KVC)以及鍵值觀察(KVO)。能夠透過觀察這些屬性來控制程式其餘的部分。如下是能夠觀察的屬性:
operations - read-only property
(工做陣列,只讀)
operationCount - read-only property
(工做數量,只讀)
maxConcurrentOperationCount - readable and writable property
(並行上限,讀寫)
suspended - readable and writable property
(是否暫停,讀寫)
name - readable and writable property
(識別名稱,讀寫)
Although you can attach observers to these properties, you should not use Cocoa bindings to bind them to elements of your application’s user interface. Code associated with your user interface typically must execute only in your application’s main thread. However, KVO notifications associated with an operation queue may occur in any thread.
雖然能夠為這些屬性添加觀察員,但不應該將之綁定至程式的用戶介面元素上。與用戶界面相關的操做必需在主線程中執行,而與KVO通知關聯的管理器有可能在任何線程中執行。
For more information about key-value observing and how to attach observers to an object, see Key-Value Observing Programming Guide.
更多有關鍵值觀察及如何為物件添加觀察員,請見Key-Value Observing Programming Guide。
Multicore Considerations
多核心的注意事項
It is safe to use a single NSOperationQueue object from multiple threads without creating additional locks to synchronize access to that object.
在多線程中使用單一的管理器(NSOperationQueue)是安全的,不要創建額外的鎖去同步取得物件。
Additional Operation Queue Behaviors
管理器附加的規則
An operation queue executes its queued operation objects based on their priority and readiness. If all of the queued operation objects have the same priority and are ready to execute when they are put in the queue—that is, their isReady method returns YES—they are executed in the order in which they were submitted to the queue. For a queue whose maximum number of concurrent operations is set to 1, this equates to a serial queue. However, you should never rely on the serial execution of operation objects. Changes in the readiness of an operation can change the resulting execution order.
管理器執行佇列的工做是基於其優先順序及就緒狀態。當工做皆被置入隊列中時,若是全部在隊列中的工做皆有相同的優先順序,並且都準備執行了—也就是表明其-(BOOL)isReady方法皆傳回YES—就會按照添加進隊列的順序執行工做。而這對於並行上限為一的隊列來說,就是連續性執行工做的隊列。然而,不應該依賴其工做的連續性執行,因為若是工做就緒狀態改變亦有可能形成最後執行順序的改變。
Tasks
方法
Managing Operations in the Queue
管理隊列中的工做
– addOperation:
– addOperations:waitUntilFinished:
– addOperationWithBlock:
– operations
– operationCount
– cancelAllOperations
– waitUntilAllOperationsAreFinished
Managing the Number of Running Operations
管理工做並行的數量
– maxConcurrentOperationCount
– setMaxConcurrentOperationCount:
Suspending Operations
暫停隊列
– setSuspended:
– isSuspended
Managing the Queue’s Name
管理隊列識別名稱
– setName:
– name
Getting Specific Operation Queues
取得特定的管理器
+ currentQueue
+ mainQueue
Class Methods
類方法
currentQueue
Returns the operation queue that launched the current operation.
傳回當前發送工做的管理器
+ (id)currentQueue
Return Value
傳回值
The operation queue that started the operation or nil if the queue could not be determined.
傳回當前發送工做的管理器,如果不能被限定則傳回nil。
Discussion
討論
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.
能夠在執行中的工做中使用此方法來取得當前執行的管理器。在工做物件之外的地方調用此方法將會毫無疑問的傳回nil。
Availability
支援
Available in OS X v10.6 and later.
支援OS X v10.6及其以後的版本。
Declared In
宣告於
NSOperation.h
mainQueue
Returns the operation queue associated with the main thread.
傳回與主線程關聯的管理器。
+ (id)mainQueue
Return Value
傳回值
The default operation queue bound to the main thread.
傳回主線程的默認管理器。
Discussion
討論
The returned queue executes operations on the main thread. The main thread’s run loop controls the execution times of these operations.
傳回的管理器是在主線程執行工做的管理器。主線程的事件循環處理器控制了這些工做物件執行的機會。
Availability
支援
Available in OS X v10.6 and later.
支援OS X v10.6及其以後版本。
Declared In
宣告於
NSOperation.h
Instance Methods
實例方法
addOperation:
Adds the specified operation object to the receiver.
添加指定的工做至隊列。
- (void)addOperation:(NSOperation *)operation
Parameters
參數
operation
The operation object to be added to the queue. In memory-managed applications, this object is retained by the operation queue. In garbage-collected applications, the queue strongly references the operation object.
要向隊列添加的工做。在自行管理記憶體的程式中,這個物件會被管理器retain。在自動回收機制的程式中,管理器會擁有這個工做物件。
Discussion
討論
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.
一個工做物件在一時間中,最多隻能在一個隊列中存在,若是添加時在其餘隊列中已存在的話,將會拋出異常NSInvalidArgumentException。同樣的,若是該工做物件已執行過或者正在執行,亦會拋出異常NSInvalidArgumentException。
Availability
支援
Available in OS X v10.5 and later.
支援OS X v10.5及其以後版本。
See Also
參見
cancel (NSOperation)
isExecuting (NSOperation)
Related Sample Code
相關範例
LinkedImageFetcher
Declared In
宣告於
NSOperation.h
addOperations:waitUntilFinished:
Adds the specified array of operations to the queue.
向隊列添加由工做組成的陣列。
- (void)addOperations:(NSArray *)ops waitUntilFinished:(BOOL)wait
Parameters
參數
ops
The array of NSOperation objects that you want to add to the receiver.
要向隊列添加的工做陣列。
wait
If YES, the current thread is blocked until all of the specified operations finish executing. If NO, the operations are added to the queue and control returns immediately to the caller.
如果YES,當前線程將會被阻塞,直到全部指定的工做皆執行完成。如果NO,指定的工做將會即刻被添加至隊列中。
Discussion
討論
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.
一個工做物件在一時間中,最多隻能在一個隊列中存在,若是添加時該工做物件已存在、已執行過或者正在執行,將會無法添加。只要參數中任何一個工做物件出現錯誤,方法將會拋出異常NSInvalidArgumentException。
Once added, the specified operation remains in the queue until its isFinished method returns YES.
一旦添加,指定的工做就會保持佇列直到其-(BOOL)isFinished方法傳回YES。
Availability
支援
Available in OS X v10.6 and later.
支援OS X v10.6及其以後的版本。
Declared In
宣告於
NSOperation.h
addOperationWithBlock:
Wraps the specified block in an operation object and adds it to the receiver.
將指定的block封入工做物件中,再將其添加至隊列。
- (void)addOperationWithBlock:(void (^)(void))block
Parameters
參數
block
The block to execute from the operation object. The block should take no parameters and have no return value.
將要執行的block。此block沒有參數以及傳回值。
Discussion
討論
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.
這個方法會將一個block封入工做物件,並添加至隊列。不應該試圖取得新建的工做物件及其類型資訊。
Once added, the specified operation remains in the queue until it its isFinished method returns YES.
一旦添加,指定的工做就會保持佇列直到其-(BOOL)isFinished方法傳回YES。
Availability
支援
Available in OS X v10.6 and later.
支援OS X v10.6及其以後版本。
See Also
參見
cancel (NSOperation)
isExecuting (NSOperation)
Related Sample Code
相關範例
QuickLookDownloader
Declared In
宣告於
NSOperation.h
cancelAllOperations
Cancels all queued and executing operations.
取消全部隊列中的工做。
- (void)cancelAllOperations
Discussion
討論
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.
這個方法會發送取消的消息至當前隊列中全部工做物件。佇列的工做將會在執行前被取消。若是工做是正在執行中的,將會由該工做物件執行注銷及停止當前工做。
Availability
支援
Available in OS X v10.5 and later.
支援OS X v10.5及其以後版本。
See Also
參見
cancel (NSOperation)
Related Sample Code
相關範例
ZipBrowser
Declared In
宣告於
NSOperation.h
isSuspended
Returns a Boolean value indicating whether the receiver is scheduling queued operations for execution.
以布林值傳回管理器是否安排工做執行。
- (BOOL)isSuspended
Return Value
傳回值
NO if operations are being scheduled for execution; otherwise, YES.
如果有安排工做執行,傳回NO,反之則傳回YES。
Discussion
討論
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.
若是想知道管理器的狀態改變,註冊一個鍵值觀察的觀察者去觀察管理器的suspended鍵值。
Availability
支援
Available in OS X v10.5 and later.
支援OS X v10.5及其以後版本。
See Also
參見
– setSuspended:
Declared In
宣告於
NSOperation.h
maxConcurrentOperationCount
Returns the maximum number of concurrent operations that the receiver can execute.
傳回管理器能夠執行並行工做的最大值。
- (NSInteger)maxConcurrentOperationCount
Return Value
傳回值
The maximum number of concurrent operations set explicitly on the receiver using the setMaxConcurrentOperationCount: method. If no value has been explicitly set, this method returns NSOperationQueueDefaultMaxConcurrentOperationCount by default.
設定於管理器的setMaxConcurrentOperationCount:方法的傳回值。若是沒有設定這個值,默認傳回NSOperationQueueDefaultMaxConcurrentOperationCount。
Availability
支援
Available in OS X v10.5 and later.
支援OS X v10.5及其以後版本。
See Also
參見
– setMaxConcurrentOperationCount:
Declared In
宣告於
NSOperation.h
name
Returns the name of the receiver.
傳回管理器的識別名稱。
- (NSString *)name
Return Value
傳回值
The name of the receiver.
管理器的識別名稱。
Discussion
討論
The default value of this string is 「NSOperationQueue <id>」, where <id> is the memory address of the operation queue. If you want to know when a queue’s name changes, configure a KVO observer to observe the name key path of the operation queue.
默認值為字串「NSOperationQueue <id>」,<id>表明管理器的記憶體位置。若是想知道管理器的識別名稱改變,註冊一個鍵值觀察的觀察者去觀察管理器的name鍵值。
Availability
支援
Available in OS X v10.6 and later.
支援OS X v10.6及其以後版本。
Declared In
宣告於
NSOperation.h
operationCount
Returns the number of operations currently in the queue.
傳回當前隊列中工做的數量。
- (NSUInteger)operationCount
Return Value
傳回值
The number of operations in the queue.
隊列中工做的數量。
Discussion
討論
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.
這個方法傳回的值為映射當前瞬間隊列的數量,而數量將會在工做完成後改變。所以,傳回數量後,真實的數量可能會改變。為此,應該將此數量用於大略的引導,不能依賴其值去計算總數或者精確計算。
Availability
支援
Available in OS X v10.6 and later.
支援OS X v10.6及其以後版本。
Declared In
宣告於
NSOperation.h
operations
Returns a new array containing the operations currently in the queue.
傳回包含當前隊列中全部工做物件的映射陣列。
- (NSArray *)operations
Return Value
傳回值
A new array object containing the NSOperation objects in the order in which they were added to the queue.
包含添加順序的工做物件組成的映射陣列。
Discussion
討論
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.
能夠在任何指定時刻使用此方法取得隊列中的工做。工做物件將會保持佇列直到其完成工做。所以,傳回的陣列會包含執行中及等待執行的工做物件。這個清單也包含了當陣列創建後完成工做的物件。
Availability
支援
Available in OS X v10.5 and later.
支援OS X v10.5及其以後的版本。
Declared In
宣告於
NSOperation.h
setMaxConcurrentOperationCount:
Sets the maximum number of concurrent operations that the receiver can execute.
設定管理器可執行並行工做的最大值。
- (void)setMaxConcurrentOperationCount:(NSInteger)count
Parameters
參數
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.
並行工做的最大值。如果想要讓管理器基於系統處理器及其餘因素使用適當的最大值,指定NSOperationQueueDefaultMaxConcurrentOperationCount便可。
Discussion
討論
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.
減少並行的工做數量並不會影響當前執行的工做。如果指定NSOperationQueueDefaultMaxConcurrentOperationCount(推薦),其最大值將會隨著系統條件改變。
Availability
支援
Available in OS X v10.5 and later.
支援OS X v10.5及其以後版本。
See Also
參見
– maxConcurrentOperationCount
Related Sample Code
相關範例
QuickLookDownloader
Declared In
宣告於
NSOperation.h
setName:
Assigns the specified name to the receiver.
指定管理器的識別名稱。
- (void)setName:(NSString *)newName
Parameters
參數
newName
The new name to associate with the receiver.
指定給管理器的新識別名稱。
Discussion
討論
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.
識別名稱提供在使用時,識別管理器的方法。工具也許會使用此值提供額外的上下文去偵錯及分析程式碼。
Availability
支援
Available in OS X v10.6 and later.
支援OS X v10.6及其以後版本。
Declared In
宣告於
NSOperation.h
setSuspended:
Modifies the execution of pending operations
修改將要執行工做的狀態。
- (void)setSuspended:(BOOL)suspend
Parameters
參數
suspend
If YES, the queue stops scheduling queued operations for execution. If NO, the queue begins scheduling operations again.
如果YES,管理器將會中止安排執行工做物件。如果NO,管理器將會繼續安排執行。
Discussion
討論
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.
工做物件只會在完成執行後移除。然而完成執行必須先開始執行。因為暫停的管理器並不會選取任何的工做去執行,因此不會移除任何工做(包括取消工做),當前佇列的工做亦不會執行。
Availability
支援
Available in OS X v10.5 and later.
支援OS X v10.5及其以後版本。
See Also
參見
– isSuspended
Declared In
宣告於
NSOperation.h
waitUntilAllOperationsAreFinished
Blocks the current thread until all of the receiver’s queued and executing operations finish executing.
阻塞當前線程直到隊列中的工做所有完成。
- (void)waitUntilAllOperationsAreFinished
Discussion
討論
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.
若是當前隊列無任何工做,此方法將會當即傳回。
Availability
支援
Available in OS X v10.5 and later.
支援OS X v10.5及其以後版本。
Related Sample Code
相關範例
From A View to A Movie
ZipBrowser
Declared In
宣告於
NSOperation.h
Constants
常量
Concurrent Operation Constants
並行工做常量
Indicates the number of supported concurrent operations.
指定支援並行工做的數量。
enum {
NSOperationQueueDefaultMaxConcurrentOperationCount = -1
};
Constants
常量
NSOperationQueueDefaultMaxConcurrentOperationCount
The default maximum number of operations is determined dynamically by the NSOperationQueue object based on current system conditions.
管理器(NSOperationQueue)基於系統狀態決定的默認最大值。
Available in OS X v10.5 and later.
支援OS X v10.5及其以後版本。
Declared in NSOperation.h.
宣告於NSOperation.h。
Declared In
宣告於
NSOperation.h
-END-