同步使用NSOperationQueue从后台线程中的多个网址下载数据

时间:2016-12-19 09:09:02

标签: objective-c synchronous nsoperationqueue

我需要同步从多个网址下载数据,这些网址需要在后台线程中完成,这样才不会影响前端活动。我想使用NSOpeartionQueue和NSURLSession。现在我使用下面的代码。

 for (int i=0;i<[tempArray count];i++) {
            CheckList * checklist = (CheckList *)[tempArray objectAtIndex:i];

        [operationQueue addOperationWithBlock:^{

            dispatch_semaphore_t semaphore = dispatch_semaphore_create(0);

            NSString * urlStr = [[BASE_URL stringByAppendingString:DOWNLOAD_CHECKLIST_METADATA_SUBURL] stringByAppendingFormat:@"%@/%d/%@",userName,[checklist.checklistId intValue],checklist.language];
            NSURL * url = [NSURL URLWithString:urlStr];
            NSLog(@"url is %@",url);

            NSURLSessionConfiguration *config = [NSURLSessionConfiguration defaultSessionConfiguration];
            NSURLSession *session = [NSURLSession sessionWithConfiguration:config];

            NSMutableURLRequest * request = [[NSMutableURLRequest alloc] initWithURL:url];
            [request setTimeoutInterval:240.0];
            [request setValue:@"Application/JSON" forHTTPHeaderField:@"Content-Type"];
            NSURLSessionDataTask * downloadTask =[session dataTaskWithRequest:request completionHandler:^(NSData * data, NSURLResponse * response, NSError * error)
                                                  {

                                                      NSString * str = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
                                                      NSLog(@"response is %@",str);
                                                  }];
            [downloadTask resume];
            [indexSetBg addIndex:[checklist.checklistId intValue]];
            // but have the thread wait until the task is done

            dispatch_semaphore_wait(semaphore, DISPATCH_TIME_FOREVER);


        }];

        }

但是,如果我想同步发送请求,是否有任何方法。在获得第一个响应后,需要发送第二个请求。你们中的任何人都可以帮我完成这件事。

非常感谢你。

1 个答案:

答案 0 :(得分:0)

你需要添加dispatch_semaphore_signal(信号量);

在NSURLSessionTask完成处理程序

的末尾
  for (int i=0;i<[tempArray count];i++) {
        CheckList * checklist = (CheckList *)[tempArray objectAtIndex:i];

    [operationQueue addOperationWithBlock:^{

        dispatch_semaphore_t semaphore = dispatch_semaphore_create(0);

        NSString * urlStr = [[BASE_URL stringByAppendingString:DOWNLOAD_CHECKLIST_METADATA_SUBURL] stringByAppendingFormat:@"%@/%d/%@",userName,[checklist.checklistId intValue],checklist.language];
        NSURL * url = [NSURL URLWithString:urlStr];
        NSLog(@"url is %@",url);

        NSURLSessionConfiguration *config = [NSURLSessionConfiguration defaultSessionConfiguration];
        NSURLSession *session = [NSURLSession sessionWithConfiguration:config];

        NSMutableURLRequest * request = [[NSMutableURLRequest alloc] initWithURL:url];
        [request setTimeoutInterval:240.0];
        [request setValue:@"Application/JSON" forHTTPHeaderField:@"Content-Type"];
        NSURLSessionDataTask * downloadTask =[session dataTaskWithRequest:request completionHandler:^(NSData * data, NSURLResponse * response, NSError * error)
                                              {

                                                  NSString * str = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
                                                  NSLog(@"response is %@",str);
                                                  dispatch_semaphore_signal(semaphore);
                                              }];
        [downloadTask resume];
        [indexSetBg addIndex:[checklist.checklistId intValue]];
        // but have the thread wait until the task is done

        dispatch_semaphore_wait(semaphore, DISPATCH_TIME_FOREVER);


    }];

    }