![](/img/trans.png)
[英]NSOperationQueue doesn't stop when [operation cancelAllOperations] is called
[英]Why doesn't my NSOperationQueue stop executing when suspended?
我有一個循環,我運行,並在每次迭代我有一個塊,我想在NSOperationQueue
上運行。 底層隊列是串行的。 此循環可能會添加數百個可能長時間運行的塊任務。 當我設置m_opQueue.suspended = YES
,塊仍將繼續執行。
我很清楚單個塊無法在中間停止,但我預計暫停NSOperationQueue
將暫停執行下一個操作,直到被掛起為假。
任何人都可以解釋我是錯了還是我如何實現我想要的?
dispatch_queue_t index_queue = dispatch_queue_create("someQueue", DISPATCH_QUEUE_SERIAL);
m_OpQueue = [[NSOperationQueue alloc] init];
m_OpQueue.underlyingQueue = index_queue;
for ( NSUInteger i = 0; i < total; i++ ) {
void (^block)(void) = ^void() {
// Do stuff.
NSLog(@"processing complete.");
};
// Effectively adds a NSBlockOperation.
[m_OpQueue addOperationWithBlock:block];
}
您描述的這種奇怪的行為(即使在隊列被掛起后,以前排隊的操作將繼續啟動)也是由您創建串行隊列的方式引起的。
通常,您可以通過設置maxConcurrentOperationCount
創建串行操作隊列:
m_OpQueue.maxConcurrentOperationCount = 1;
如果你這樣做(不需要設置underlyingQueue
),你會看到預期的行為。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.