0

I'm trying to download information in background from a url. I've read about GCD, Runloops and threads and decided that dispatc_async is my way to go. After receiving data I aalso want to update the gui. But... the NSUrlConnection don't seem to start at all. The delegate don't receive any calls. I'v used this NSUrlRequest and NSUrlConnection in a synchronous way and the delegate got the data excpected.

Here is my code, a method in a viewcontroller;

- (void)dispatch: (NSURLRequest *) pRequest respondTo: (VivaQuery *) pQuery   {

    dispatch_queue_t downloadQueue =          dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
    dispatch_async(downloadQueue, ^{

        NSURLConnection *tConnectionResponse =[[NSURLConnection alloc] initWithRequest: pRequest delegate: pQuery];
        dispatch_async(dispatch_get_main_queue(), ^{
             NSLog(@"Got to main thread.");
             [pQuery requestEnd]; // Will update gui, i e aUIView setNeedsDisplay 
         });
    });
} 

Anyone got an idea? Thanks in advance.

Kind regards, Jan Gifvars Stockholm

Martin R
  • 529,903
  • 94
  • 1,240
  • 1,382
Jan Gifvars
  • 111
  • 2
  • 8

1 Answers1

0

NSUrlConnection does its work asynchronously on it's own background thread so you do not need to create it in a background thread.

Kevin
  • 16,696
  • 7
  • 51
  • 68
  • Ok, thank you. I thougt so too... but then I have somproblem updating the gui from that thread started by NSUrlConnection. I post another question on that issue. Thanks again. – Jan Gifvars Sep 01 '13 at 19:30