0
votes

I followed below code to establish https connection in my application to fetch some data in xml format in my iphone (iOS4) application.

  1. Creating a connection using NSURLConnection
  2. connection:didReceiveResponse: implementation
  3. connection:didReceiveData
  4. connectionDidFailWithError
  5. connectionDidFinishLoading

I referred exacly following to establish connection: http://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/URLLoadingSystem/Tasks/UsingNSURLConnection.html

Everything works fine for a single connection. But if I try 2 (or more) connections simultaneously (each connection being in separate thread), and if first connection is being served, then second connection gets discarded.

The second connection never gets queued up. It is simply discarded. Only first is served.

What can be the issue? My requirement is to create multiple (at least 2) connection simultaneously.

Also, Please share sample code or link or example if possible. Thanks

1

1 Answers

0
votes

It's not a simple task to run NSURLConnection in separate thread. If you just execute NSURLConnection in separate thread, you can not handle delegate methods because your thread is finished before your code actually executed.


After you initialized the NSURLConnection object, you actually need to put the class in an infinite loop to make sure the class is still alive when the delegate methods are hit.

NSURLConnection *conn = [[NSURLConnection alloc] initWithRequest:urlReq delegate:self];

while(!finished) {
    [[NSRunLoop currentRunLoop] runMode:NSDefaultRunLoopMode beforeDate:[NSDate distantFuture]];
}

- (void) connectionDidFinishLoading:(NSURLConnection *)connection
{
    finished = TRUE;
}

See this article - NSURLConnection in its own thread