2009-07-18 19 views
5

是否有一種安全的方法來改變已經在NSOperationQueue中排隊的NSOperation的優先級?如果操作還沒有開始,那麼沒有問題,但是如果它已經在執行,我會得到一個NSInvalidArgumentException。安全的方式來改變一個NSOperation的優先級

+1

你能告訴我們異常的堆棧跟蹤和內容是什麼?目前有點難以診斷 – 2009-07-19 12:57:02

回答

11

你不應該改變NSOperation一旦它已經入隊。根據蘋果的Threading Programming Guide

重要提示:您永遠不應該修改 操作對象時,它已經經過 添加到隊列中。在等待 隊列時,操作可以在任何時間在 執行。 正在執行時更改其狀態可能會產生不利影響。您可以使用 操作對象的方法來確定 操作是否正在運行,等待 運行,或者已經完成。

+0

更新後的鏈接:http://developer.apple.com/library/mac/documentation/General/Conceptual/ConcurrencyProgrammingGuide/OperationObjects/OperationObjects.html#//apple_ref/doc/uid/TP40008091 -CH101-SW38 – 2013-06-28 01:11:43

0

docs上,沒有關於在操作開始後沒有改變優先級的警告,也沒有關於引發異常的提及。

事實上,改變運行操作的優先級根本沒有效果。也許你在別處做錯了什麼?

1

根據Concurrency Programming Guide我們應該能夠更改優先級

For operations added to a queue, execution order is determined first by the 
readiness of the queued operations and then by their relative priority. 
Readiness is determined by an operation’s dependencies on other operations, 
but the priority level is an attribute of the operation object itself. 
By default, all new operation objects have a 「normal」 priority, but you 
can increase or decrease that priority as needed by calling the object’s 
setQueuePriority: method.