将GCD转换为NSOperationqueue

时间:2017-03-16 06:37:45

标签: ios objective-c nsoperationqueue

如果在使用NSOperation时可以取消任务,而如果我使用GCD,那么一旦我将任务分配到队列然后我们无法取消它,因此我想知道我怎么能转换我在GCDNSOperation

中的以下实施
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;
       });
 })

1 个答案:

答案 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];