1. NSOperationQueue compared to GCD

    1. GCD is a more efficient API for C.
    2. NSOperationQueue can set maximum concurrency, priority, and dependencies to adjust the order of task execution.
    3. The NSOperationQueue is an object of the OC that can do KVO listening to see if execution is in progress or if it has completed, or if it is cancelled.
    4. NSOperationQueue can cancel, suspend, and resume operations that have not yet been performed.
    5. If the COMMUNIST party can solve the problem, we try to use the Communist Party. If GCD cannot solve the problem, we use NSOperationQueue.
  2. Simple use of NSOperationQueue

  NSOperationQueue *operationQueue = [[NSOperationQueue alloc] init];
    
    //1.
    [operationQueue addOperationWithBlock:^{
        NSLog(@"curThread = %@",[NSThread currentThread]);
        
        [[NSOperationQueue mainQueue] addOperationWithBlock:^{
            NSLog(@"mainOperationQueue");
        }];
    }];
    
    //2.
    NSBlockOperation *blockOperation = [NSBlockOperation blockOperationWithBlock:^{
        NSLog(@"curThread = %@",[NSThread currentThread]);
    }];
    [operationQueue addOperation:blockOperation];
    
    //3.
    NSInvocationOperation *invocationOpeartion = [[NSInvocationOperation alloc] initWithTarget:self selector:@selector(demo1:) object:@"invocationOperation"];
    [operationQueue addOperation:invocationOpeartion];
    
Copy the code
  1. NSOperationQueue differs from the GCD API

    1. Can control the maximum number of concurrent requests

      1. Create asynchronous concurrent queues and asynchronous serial queues
      2. code
      NSOperationQueue *operationQueue = [[NSOperationQueue alloc] init]; // Control the maximum number of concurrent requests, if not written equivalent to asynchronous concurrent queue. If write 1 equals asynchronous serial queue. If write other equivalent to asynchronous concurrent queue operationQueue maxConcurrentOperationCount = 1;for (int i = 0; i < 10; i++) {
              [operationQueue addOperationWithBlock:^{
                  NSLog(@"curThread = %@, i = %d",[NSThread currentThread],i);
              }];
          }
      Copy the code
    2. You can add dependencies

      1. Complex dependencies can be added
      2. code
        operationQueue = [[NSOperationQueue alloc] init];
          
      
          NSBlockOperation *block1 = [NSBlockOperation blockOperationWithBlock:^{
              NSLog(@"1");
          }];
          
          NSBlockOperation *block2 = [NSBlockOperation blockOperationWithBlock:^{
               NSLog(@"2");
          }];
          
          NSBlockOperation *block3 = [NSBlockOperation blockOperationWithBlock:^{
               NSLog(@"3");
          }];
          
          [block1 addDependency:block3];
          [block2 addDependency:block3];
          
          [operationQueue addOperation:block1];
          [operationQueue addOperation:block2];
          [operationQueue addOperation:block3];
      Copy the code
      1. Set the priority of a task
        NSOperationQueue *operationQueue = [[NSOperationQueue alloc] init];
          
          NSBlockOperation *blockOperation1 = [NSBlockOperation blockOperationWithBlock:^{
              NSLog(@"CurThread = %@, task 1",[NSThread currentThread]);
          }];
          
          NSBlockOperation *blockOperation2 = [NSBlockOperation blockOperationWithBlock:^{
              NSLog(@"CurThread = %@, task 2",[NSThread currentThread]);
          }];
          
          NSBlockOperation *blockOperation3 = [NSBlockOperation blockOperationWithBlock:^{
              NSLog(@"CurThread = %@, task 3",[NSThread currentThread]);
          }];
          
          [blockOperation1 setQueuePriority:NSOperationQueuePriorityLow];
          [blockOperation1 setQueuePriority:NSOperationQueuePriorityNormal];
          [blockOperation1 setQueuePriority:NSOperationQueuePriorityHigh];
          
          [operationQueue addOperation:blockOperation1];
          [operationQueue addOperation:blockOperation2];
          [operationQueue addOperation:blockOperation3];
      Copy the code
      1. KVO
      - (void)viewDidLoad { [super viewDidLoad]; NSOperationQueue *operationQueue = [[NSOperationQueue alloc] init]; BlockOperationWithBlock :^{sleep(3.0); NSLog(@)"CurThread = %@, task 3",[NSThread currentThread]);
          }];
          [operationQueue addOperation:blockOperation3];
          
          [blockOperation3 addObserver:self forKeyPath:@"finished" options:NSKeyValueObservingOptionOld|NSKeyValueObservingOptionNew context:nil];
      }
      - (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary<NSKeyValueChangeKey,id> *)change context:(void *)context{
          if ([keyPath isEqualToString:@"finished"] && object == blockOperation3) {
              NSLog(@"change = %@",change); }}Copy the code
      1. Cancel, suspend, resume operations
      - (void)viewDidLoad {
          [super viewDidLoad];
          
          operationQueue = [[NSOperationQueue alloc] init];
          operationQueue.maxConcurrentOperationCount = 2;
          for(int i = 0; i < 100; I++) {[operationQueue addOperationWithBlock:^{sleep(1.0); NSLog(@"i = %d",i); }]; } } - (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{ //1. Cancels all operations that have not yet been performed (operationQueue cancelAllOperations) [operationQueue cancelAllOperations]; //2. Suspend and resume tasksif (operationQueue.suspended) {
              operationQueue.suspended = NO;
          }else{ operationQueue.suspended = YES; }}Copy the code