NSOperation 实现多任务执行完毕,再执行下一个任务

时间:2021-03-02 16:55:42
 //任务1 中的任务都执行完了才执行第二个任务 和队列组    dispatch_group_t group = dispatch_group_create(); 相似的功能
    
    NSBlockOperation *block = [NSBlockOperation blockOperationWithBlock:^{
        
        dispatch_queue_t queue = dispatch_queue_create("11", DISPATCH_QUEUE_CONCURRENT);
        
       dispatch_async(queue, ^{
           NSLog(@"1");
       });
        dispatch_async(queue, ^{
            NSLog(@"2");
        });
        dispatch_async(queue, ^{
            NSLog(@"3");
        });
    }];
    //任务2
    NSBlockOperation *block2 = [NSBlockOperation blockOperationWithBlock:^{
        NSLog(@"终于可以进行4了");
    }];
    
    //添加依赖 等block执行完了 ,才执行block2
    [block2 addDependency:block];
    
    NSOperationQueue *operationQueue = [[NSOperationQueue alloc] init];
    operationQueue.maxConcurrentOperationCount = 1;
    [operationQueue addOperations:@[block2, block] waitUntilFinished:NO];