Search code examples
iphoneurlasynchronousconnectionnsurlrequest

Multiple asynchronous URL requests


My iPhone application has been "lagging" or rather "frozen" when it got past the start up screen. I think this is due to the registration for remote push notifications is send as a synchronous request and therefore I would like to change this to asynchronous. This is a problem since I am already sending one asynchronous request for retrieving some data and save it to the phone. So, I would like to send both of these requests asynchronously and have them do two different things in - (void)connectionDidFinishLoading:(NSURLConnection *)connection. Therefore I need to know which of the two connections that finished.

Is there any way to do this? Would there be any way to distinguish by the URL of the finished connection? Actually, I thought it would be as easy as set a tag and check this in - (void)connectionDidFinishLoading:(NSURLConnection *)connection but this does not seem to be possible.

Does anyone know how I can do this?


Solution

  • As Kalle said, the best thing to do is a class that handles the connection, parses the response, and returns the data in a pretty delegate function.

    However if you must for some reason make 2 NSURLConnections with the same delegate, what you want to do is save references to them both in class ivars. Something like NSURLConnection *pushNotificationConnection; and NSURLConnection *someOtherConnection;

    Then, your didReceiveData function should look something like:

    -(void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
    {
        if (connection == pushNotificationConnection)
        {
            // handle the push notification related data
        }
        else if (connection == someOtherConnection)
        {
            // handle the other connection
        }
    }