每次開啟一個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];
}