I have a loop which I run and in each iteration I have a block that I want run on an NSOperationQueue
. The underlying queue is serial. This loop could add hundreds of potentially long running block tasks. When I set m_opQueue.suspended = YES
the blocks will still keep executing.
我有一个循环,在每次迭代中,我有一个块我想在NSOperationQueue上运行。底层队列是串行的。这个循环可以添加数百个潜在的长时间运行的块任务。当我设置m_opQueue。暂停=是的,块将继续执行。
I'm well aware that a single block cannot stop right in the middle, but I expected that pausing the NSOperationQueue
would simply not execute the next operation until suspended was false.
我很清楚,单个块不能在中间停止,但我希望暂停NSOperationQueue将不会执行下一个操作,直到挂起为false。
Can anyone explain whether I'm wrong or how I achieve what I want?
有人能解释我是错的还是我想要的吗?
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];
}
1 个解决方案
#1
3
This curious behavior you describe (where previously enqueued operations will continue to start even after the queue has been suspended), is caused by how you created the serial queue.
您所描述的这种奇怪的行为(以前进入队列的操作在队列被挂起之后仍然继续启动)是由您如何创建串行队列引起的。
Generally, you create a serial operation queue by setting maxConcurrentOperationCount
:
通常,通过设置maxConcurrentOperationCount创建一个串行操作队列:
m_OpQueue.maxConcurrentOperationCount = 1;
If you do that (no need to set underlyingQueue
), you see the expected behavior.
如果您这样做(不需要设置underlyingQueue),您将看到预期的行为。
#1
3
This curious behavior you describe (where previously enqueued operations will continue to start even after the queue has been suspended), is caused by how you created the serial queue.
您所描述的这种奇怪的行为(以前进入队列的操作在队列被挂起之后仍然继续启动)是由您如何创建串行队列引起的。
Generally, you create a serial operation queue by setting maxConcurrentOperationCount
:
通常,通过设置maxConcurrentOperationCount创建一个串行操作队列:
m_OpQueue.maxConcurrentOperationCount = 1;
If you do that (no need to set underlyingQueue
), you see the expected behavior.
如果您这样做(不需要设置underlyingQueue),您将看到预期的行为。