监听NSOperationQueue 队列任务完成

// KVO,监听 operationCount,等于0说明完成

  • (NSOperationQueue *)operationQueue {
    if (!_operationQueue) {
    _operationQueue = [[NSOperationQueue alloc] init];
    [_operationQueue addObserver:self forKeyPath:@"operationCount" options:0 context:nil];
    }
    return _operationQueue;
    }
  • (void)observeValueForKeyPath:(NSString *)keyPath
    ofObject:(id)object
    change:(NSDictionary *)change
    context:(void *)context
    {
    if (object == self.operationQueue && [keyPath isEqualToString:@"operationCount"])
    {

      // 等于0表示完成
      if (0 == self.operationQueue.operations.count)
      {
    
      }
    

    }
    else
    {
    [super observeValueForKeyPath:keyPath ofObject:object change:change context:context];
    }
    }

你可能感兴趣的:(监听NSOperationQueue 队列任务完成)