GCD 加信号量串行进行多次网络请求操作

printf("处理前创建信号量,开始循环异步请求操作\n");

// 1.创建一个串行队列,保证for循环依次执行
        dispatch_queue_t serialQueue = dispatch_queue_create("serialQueue", DISPATCH_QUEUE_SERIAL);

// 2.异步执行任务
dispatch_async(serialQueue, ^{
    // 3.创建一个数目为1的信号量,用于“卡”for循环,等上次循环结束在执行下一次的for循环
    dispatch_semaphore_t sema = dispatch_semaphore_create(1);

    for (int i = 0; i<5; i++) {
        // 开始执行for循环,让信号量-1,这样下次操作须等信号量>=0才会继续,否则下次操作将永久停止
        dispatch_semaphore_wait(sema, DISPATCH_TIME_FOREVER);
        printf("信号量等待中\n");
        // 模拟一个异步任务
        NSMutableURLRequest *urlRequest = [[NSMutableURLRequest alloc] initWithURL:[NSURL URLWithString:@"https://github.com"]];
        NSURLSession *session = [NSURLSession sharedSession];
        NSURLSessionDataTask *dataTask = [session dataTaskWithRequest:urlRequest completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
        // 本次for循环的异步任务执行完毕,这时候要发一个信号,若不发,下次操作将永远不会触发
            [tampArray addObject:@(i)];
            NSLog(@"本次耗时操作完成,信号量+1 %@\n",[NSThread currentThread]);
            dispatch_semaphore_signal(sema);

        }];
        [dataTask resume];

    }

    NSLog(@"其他操作");
    for (NSNumber *num in tampArray) {
        NSLog(@"所有操作完成后的操作--->   %@\n",num);
    }

});

双重信号量


        //上传最近的3个log文件,
        //至少要3个,因为最后一个是crash的记录信息,另外2个是防止其中后一个文件只写了几行代码而不够分析
        NSArray *logFilePaths = [fileLogger.logFileManager sortedLogFilePaths];
        
        NSUInteger logCounts = logFilePaths.count;
        
        if (logCounts >= 3) {
            
            dispatch_queue_t queue = dispatch_queue_create(0, DISPATCH_QUEUE_SERIAL);

            dispatch_semaphore_t semaphore = dispatch_semaphore_create(1);
            
            dispatch_async(queue, ^{
             
                for (NSUInteger i = 0; i < 3; i++) {
                    
                    dispatch_semaphore_wait(semaphore, DISPATCH_TIME_FOREVER);
                    
                    NSString *logFilePath = logFilePaths[i];
                    
                    //上传服务器
                    
                    NSData *file_total = [NSData dataWithContentsOfFile:logFilePath];
                    
                    KKLogDebug(@"上传服务器 logFilePath %@  %@",logFilePath,file_total);
                    
                    if(file_total.length > 0){
                        
                        NSMutableArray *dataArrM = [self uploadLogFileArrWithData:file_total];
                        
                        if(dataArrM.count > 0){

                            dispatch_queue_t queue_upload = dispatch_queue_create(0, DISPATCH_QUEUE_SERIAL);
                            
                            dispatch_semaphore_t semaphore_upload = dispatch_semaphore_create(1);
                            
                            dispatch_async(queue_upload, ^{

                                for(int j = 0; j < dataArrM.count; j++){

                                    dispatch_semaphore_wait(semaphore_upload, DISPATCH_TIME_FOREVER);
            
                                    NSData *data = dataArrM[i];
                                    
                                    KKLogDebug(@"+++++++++ %@ == %zd  %@ totalSize=%@",[NSString stringWithFormat:@"%lu",data.length * j],dataArrM.count,[NSThread currentThread],[NSString stringWithFormat:@"%zd",data.length]);
                                    
                                    [[[KKUtility fetchUploadFileLogWithContent:dataArrM[j] fileName:logFilePath ready:[NSString stringWithFormat:@"%zd",data.length * j+1] totalSize:[NSString stringWithFormat:@"%zd",data.length] status:@"0"] deliverOnMainThread] subscribeNext:^(NSDictionary *result) {
                                        
                                        if (j == dataArrM.count - 1) {
                                            dispatch_semaphore_signal(semaphore);
                                        }
                                        
                                        dispatch_semaphore_signal(semaphore_upload);
                                        
                                        KKLogDebug(@"result  %@",result);
                                        
                                    } error:^(NSError *error) {
                                        
                                        dispatch_semaphore_signal(semaphore);
                                        dispatch_semaphore_signal(semaphore_upload);
                                        KKLogError(@"%@",error);
                                    }];
                                }
                            });
                        }
                    }
                    else {
                        dispatch_semaphore_signal(semaphore);
                    }
                }
              });
              
        }

文件切片:

- (NSMutableArray *)uploadLogFileArrWithData:(NSData *)file_total
{
     NSUInteger allLength = file_total.length;
                          
      NSUInteger subs = 4096;//要切片的大小,

      NSInteger index = 0;//起始位置

      NSMutableArray *dataArray =[NSMutableArray new];
      
      do {

          if (allLength>subs) {
              
              NSRange range =NSMakeRange(index*subs, subs);
              
              index++;
              
//              KKLogDebug(@"%@",NSStringFromRange(range));
              
              [dataArray addObject:[file_total subdataWithRange:range]];
              
              allLength = allLength - subs;

          }else{
              
              NSRange range = NSMakeRange(index*subs, allLength);
              
//              KKLogDebug(@"%@",NSStringFromRange(range));
              
              [dataArray addObject:[file_total subdataWithRange:range]];

              allLength = 0;
          }

          
      } while (allLength>0);
      
//      KKLogDebug(@" dataArray %@",dataArray);//最后得到切片的结果,数组里面是NSData对象
     return dataArray;
}

你可能感兴趣的:(GCD 加信号量串行进行多次网络请求操作)