gcd实现多个请求结束后在做其他事情

//第一种(有两种实现) 通过信号量控制多个请求结束后回到主线程处理操作
//实现1
-(void)testGcd{
    dispatch_queue_t conQueue = dispatch_queue_create("qu", DISPATCH_QUEUE_CONCURRENT);
    NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString: @"https://www.baidu.com"]];
    dispatch_semaphore_t  semaphore = dispatch_semaphore_create(0);
    for (int i=0; i<5; i++) {
        dispatch_async(conQueue, ^{
            NSURLSessionDataTask *task = [[NSURLSession sharedSession] dataTaskWithRequest:request completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
                NSLog(@"---%d",i);
                dispatch_semaphore_signal(semaphore);
            }];
            [task resume];
        });
    }
    dispatch_async(dispatch_get_main_queue(), ^{
        dispatch_semaphore_wait(semaphore, DISPATCH_TIME_FOREVER);
        dispatch_semaphore_wait(semaphore, DISPATCH_TIME_FOREVER);
        dispatch_semaphore_wait(semaphore, DISPATCH_TIME_FOREVER);
        dispatch_semaphore_wait(semaphore, DISPATCH_TIME_FOREVER);
        dispatch_semaphore_wait(semaphore, DISPATCH_TIME_FOREVER);
        NSLog(@"end");
    });
}
//实现2
-(void)testGcd{
    dispatch_queue_t conQueue = dispatch_queue_create("qu", DISPATCH_QUEUE_CONCURRENT);
    NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString: @"https://www.baidu.com"]];
   __block int count = 0;
    dispatch_semaphore_t  semaphore = dispatch_semaphore_create(0);
    for (int i=0; i<5; i++) {
        dispatch_async(conQueue, ^{
            NSURLSessionDataTask *task = [[NSURLSession sharedSession] dataTaskWithRequest:request completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
                count++;
                NSLog(@"---%d,count=%d",i,count);
                if (count == 5) {//这里用i==4 判断会出现问题 导致end不是最好输出
                    dispatch_semaphore_signal(semaphore);
                }
            }];
            [task resume];
        });
    }
    dispatch_semaphore_wait(semaphore, DISPATCH_TIME_FOREVER);
    dispatch_async(dispatch_get_main_queue(), ^{
        NSLog(@"end");
    });
}
//输出
2021-08-13 23:29:05.464840+0800 多线程[2089:162308] ---1,count=1
2021-08-13 23:29:05.467028+0800 多线程[2089:162308] ---3,count=2
2021-08-13 23:29:05.469865+0800 多线程[2089:162308] ---0,count=3
2021-08-13 23:29:05.472069+0800 多线程[2089:162309] ---2,count=4
2021-08-13 23:29:05.479075+0800 多线程[2089:162308] ---4,count=5
2021-08-13 23:29:05.479376+0800 多线程[2089:161844] end
//第二种通过dispatch_group_t +  并发队列 +  dispatch_group_enter + dispatch_group_leave
//实现多个网络并发请求 在接口结束后去做想做的事情
-(void)testGcd{
    dispatch_queue_t conQueue = dispatch_queue_create("qu", DISPATCH_QUEUE_CONCURRENT);
    NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString: @"https://www.baidu.com"]];
    dispatch_group_t group = dispatch_group_create();
    for (int i=0; i<5; i++) {
        dispatch_group_enter(group);
        dispatch_async(conQueue, ^{
            NSURLSessionDataTask *task = [[NSURLSession sharedSession] dataTaskWithRequest:request completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
                NSLog(@"---%d",i);
                dispatch_group_leave(group);   
            }];
            [task resume];
        });
    }
    dispatch_group_notify(group, dispatch_get_main_queue(), ^{
        NSLog(@"end");
    });
}
//输出结果
2021-08-13 23:03:50.624630+0800 多线程[1701:137728] ---2
2021-08-13 23:03:50.627370+0800 多线程[1701:137728] ---3
2021-08-13 23:03:50.628828+0800 多线程[1701:137728] ---1
2021-08-13 23:03:50.630811+0800 多线程[1701:137727] ---4
2021-08-13 23:03:50.637098+0800 多线程[1701:137728] ---0
2021-08-13 23:03:50.637292+0800 多线程[1701:137287] end
//第三种 信号量+NSOperation
-(void)testGcd{
    dispatch_semaphore_t sema = dispatch_semaphore_create(0);
    NSOperationQueue *queue = [[NSOperationQueue alloc]init];
    NSBlockOperation *operation1 = [NSBlockOperation blockOperationWithBlock:^{
        NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString: @"https://www.baidu.com"]];
        NSURLSessionDataTask *task = [[NSURLSession sharedSession] dataTaskWithRequest:request completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
            NSLog(@"任务1");
            dispatch_semaphore_signal(sema);
        }];
        [task resume];
    }];
    NSBlockOperation *operation2 = [NSBlockOperation blockOperationWithBlock:^{
        NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString: @"https://www.baidu.com"]];
        NSURLSessionDataTask *task = [[NSURLSession sharedSession] dataTaskWithRequest:request completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
            NSLog(@"任务2");
            dispatch_semaphore_signal(sema);
        }];
        [task resume];
    }];
    
    NSBlockOperation *operation3 = [NSBlockOperation blockOperationWithBlock:^{
        dispatch_semaphore_wait(sema, DISPATCH_TIME_FOREVER);
        dispatch_semaphore_wait(sema, DISPATCH_TIME_FOREVER);
        NSLog(@"任务3");
    }];
    [operation3 addDependency:operation1];
    [operation3 addDependency:operation2];
    [queue addOperations:@[operation1,operation3,operation2] waitUntilFinished:NO];
}
//输出结果
2021-08-13 23:56:41.803946+0800 多线程[2543:197241] 任务2
2021-08-13 23:56:41.814839+0800 多线程[2543:197415] 任务1
2021-08-13 23:56:41.815144+0800 多线程[2543:197243] 任务3

你可能感兴趣的:(gcd实现多个请求结束后在做其他事情)