开发者

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 asynchro开发者_如何学编程nous. 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?


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
    }
}


A clean way to do this is to actually have a separate class handle each request. Or rather, you have a class which is supposed to perform the request, get the data, then send them back (via delegation) to the main class once it's done.

You would thus have two classes, e.g. PushNotificationRequestor and SomeLoader. Each would create and maintain their own separate HTTP (or whatever type it is) requests and would each have their own separate connectionDidFinishLoading: etc. methods.


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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜