Downloading data from a number of urls in background thread using NSOperationQueue synchronously

77 views Asked by At

I need to download data from a number of urls synchronously which needs to be done in the background thread so that it won't affect the front end activities.I would like to use NSOpeartionQueue and NSURLSession. Right now I am using the below code.

 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);


        }];

        }

But if I want to send the request synchronously, is there any way for that.Means after getting 1st response ,second request needs to be sent. Can anyone of you please help me to accomplish this.

Thank you very much in advance.

1

There are 1 answers

8
Marat Ibragimov On

you need to add dispatch_semaphore_signal(semaphore);

in the end of your NSURLSessionTask completion handler

  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);


    }];

    }