3

What I am trying to do is getting response for following method

 - (void) connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response { }

after calling this

NSURLConnection *conn = [[NSURLConnection alloc] initWithRequest:request delegate:self];
[conn scheduleInRunLoop:[NSRunLoop mainRunLoop] forMode:NSDefaultRunLoopMode];
[conn start];

inside a

dispatch_async();

But the connection method is not calling. But when I run the NSURLConnection code outside the dispatch_async it call the method.

What is the reason for that and how can I correct it? Is that because delegate refers to self and self refers the the background thread but not the UIViewController class itself?

Jonathan Hall
  • 75,165
  • 16
  • 143
  • 189
AnujAroshA
  • 4,623
  • 8
  • 56
  • 99
  • What thread are you on when calling `dispatch_async` and on what thread do you want to dispatch it? – Levi May 06 '14 at 11:35
  • which dispatch queue is you using in `dispatch_async()`? – Inder Kumar Rathore May 06 '14 at 11:41
  • When I am calling `NSURLConnection` I am inside `dispatch_async()`. By the way by calling `[self performSelectorOnMainThread:@selector(myBgWork) withObject:Nil waitUntilDone:NO];` this and put my `NSURLConnection` code inside `myBgWork` gives me the response for `didReceiveResponse`. But now I am confuse because I feel my background work again calling to main thread and it will slow my application. Is that so? – AnujAroshA May 06 '14 at 11:47

1 Answers1

0

Since you're creating a connection to be scheduled in a loop and started later, make sure to use the rendition of initWithRequest with the startImmediately parameter set to NO. Right now, you're starting the connection twice. And, more significantly, you're starting it before you scheduled it in the main run loop.

To remedy this, specify startImmediately as NO:

NSURLConnection *conn = [[NSURLConnection alloc] initWithRequest:request delegate:self startImmediately:NO];
[conn scheduleInRunLoop:[NSRunLoop mainRunLoop] forMode:NSDefaultRunLoopMode];
[conn start];

Having said that, I see no need to dispatch this code to the background queue at all (because initWithRequest runs asynchronously and will have no discernible impact on the app performance). The only time I use the above pattern is when I'm wrapping my NSURLConnection requests in some custom NSOperation subclass (in which case, the above pattern is very useful). But in this case, it is unnecessary.

Rob
  • 415,655
  • 72
  • 787
  • 1,044
  • Thank yo for the response. I used `dispatch_async()` because I'm having a file zipping process too which will take a considerable time. – AnujAroshA May 07 '14 at 03:15
  • @AnujAroshA Then you can dispatch just that zipping process to a background queue. But there's no point in dispatching the start of the connection to a background queue, especially if you're going to turn around and schedule it in the main run loop (which means all of the delegate methods are called on the main thread, even though you instantiate the connection from a background queue). Even if it didn't run on the main thread, there's no reason not to. Just dispatch that slow zipping code to a background queue. – Rob May 07 '14 at 04:40