2 developerwab developerWab 于 2013.07.09 16:25 提问

终止异步DISPATCH_QUEUE

每次开启一个DISPATCH_QUEUE需要三四分钟。我需要一个按钮可以终止队列。怎么实现?

dispatch_sync(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0), ^{
        @synchronized(self)
        {
            for (int i = 0; i < (4000); i++) {
                         (Some methods)
            }
        }
});

能否终止或取消此线程?

1个回答

redCoral_
redCoral_   2013.07.09 16:59
已采纳

使用dispatch_sync阻塞了主线程

你应该用 NSOperationQueue

NSOperationQueue *_myQueue; //instance var

_myQueue = [[NSOperationQueue alloc] init]; //init it

_myQueue.suspended = (buttonPressed) ? YES : NO; //toggle it like you need

for (int i = 0; i < (4000); i++) {
   [_queue addOperationWithInvocation:NSInvocation for method to call];  
}
Csdn user default icon
上传中...
上传图片
插入图片