如果在使用NSOperation
时可以取消任务,而如果我使用GCD
,那么一旦我将任务分配到队列然后我们无法取消它,因此我想知道我怎么能转换我在GCD
到NSOperation
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_BACKGROUND, 0), ^(void) {
[self addAllImages];
dispatch_sync(dispatch_get_main_queue(), ^(void) {
[self pageControlSetUp];
self.fullScreenImageView.hidden = YES;
});
})
答案 0 :(得分:2)
NSOperationQueue *queue = [[NSOperationQueue alloc] init];
NSBlockOperation *operation = [NSBlockOperation blockOperationWithBlock:^{
[self addAllImages];
dispatch_sync(dispatch_get_main_queue(), ^(void) {
[self pageControlSetUp];
self.fullScreenImageView.hidden = YES;
});
}];
[queue addOperation:operation];
//cancel operation
[operation cancel];
//or to cancell all operations
[queue cancelAllOperations];