繁体   English   中英

为什么我的NSOperationQueue在暂停时不会停止执行?

[英]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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM