title: Running multiple background threads iOS
body: <p>Is it possible to run multiple background threads to improve performance on iOS . Currently I am using the following code for sending lets say 50 network requests on background thread like this:</p>
  
  <pre><code> dispatch_async(dispatch_get_global_queue( DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^(void){
              // send 50 network requests 
   });
  </code></pre>
  
  <p><strong>EDIT:</strong></p>
  
  <p>After updating my code to something like this no performance gain was achieved :(   Taken from <a href="https://stackoverflow.com/questions/16054301/asynchronous-url-requests-inside-dispatch-async">here</a></p>
  
  <pre><code>dispatch_queue_t fetchQ = dispatch_queue_create("Multiple Async Downloader", NULL);
  dispatch_group_t fetchGroup = dispatch_group_create();
  
  // This will allow up to 8 parallel downloads.
  dispatch_semaphore_t downloadSema = dispatch_semaphore_create(8);
  
  // We start ALL our downloads in parallel throttled by the above semaphore.
  for (NSURL *url in urlsArray) {
      dispatch_group_async(fetchGroup, fetchQ, ^(void) {
          dispatch_semaphore_wait(downloadSema, DISPATCH_TIME_FOREVER);
          NSMutableURLRequest *headRequest = [NSMutableURLRequest requestWithURL:url  cachePolicy: NSURLRequestUseProtocolCachePolicy timeoutInterval:60.0];
          [headRequest setHTTPMethod: @"GET"];
          [headRequest addValue: cookieString forHTTPHeaderField: @"Cookie"];
  
           NSOperationQueue *queue = [[[NSOperationQueue alloc] init] autorelease];
          [NSURLConnection sendAsynchronousRequest:headRequest
                                             queue:queue // created at class init
                                 completionHandler:^(NSURLResponse *response, NSData *data, NSError *error){
                                     // do something with data or handle error
                                     NSLog(@"request completed");
                                 }];
          dispatch_semaphore_signal(downloadSema);
      });
  }
  
  // Now we wait until ALL our dispatch_group_async are finished.
  dispatch_group_wait(fetchGroup, DISPATCH_TIME_FOREVER);
  
  // Update your UI
  dispatch_sync(dispatch_get_main_queue(), ^{
      //[self updateUIFunction];
  });
  
  // Release resources
  dispatch_release(fetchGroup);
  dispatch_release(downloadSema);
  dispatch_release(fetchQ);
  </code></pre>