每次开启一个DISPATCH_QUEUE需要三四分钟。我需要一个按钮可以终止队列。怎么实现?
dispatch_sync(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0), ^{
@synchronized(self)
{
for (int i = 0; i < (4000); i++) {
(Some methods)
}
}
});
能否终止或取消此线程?
使用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];
}