将 GCD 转换为 NSOperationqueue
Convert GCD to NSOperationqueue
如果使用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;
});
})
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];
如果使用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;
});
})
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];