0

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?

simonbs
  • 7,932
  • 13
  • 69
  • 115
  • This is incredibly easy to do with **ASIHttpRequest** which is the most used 3rd party library in all of iPhone. Your problem is solved. – Fattie Apr 10 '11 at 10:49

3 Answers3

2

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
    }
}
Zaky German
  • 14,324
  • 4
  • 25
  • 31
  • That sounds smart in my ears. I am quite new to Objective-C. How would you define your connections to begin with to be able to compare them like that? – simonbs Apr 10 '11 at 14:05
  • @SimonBS in the .h file of your viewController (or whatever class it is that fires up the connection and acts as a delegate - where you parse the data from the connections and such) define *pushNotificationConnection; NSURLConnection *someOtherConnection; Then when firing the connections, set the references appropriately. For example pushNotificationConnection = [NSURLConnection connectionWithRequest:myPushNotificationsRequest]; do the same with the other connection. Then compare the connection returned from the delegate function with those like in the example in my answer – Zaky German Apr 10 '11 at 14:18
  • Of course! Thank you very much. This solved my issue. My application does not "freeze" on start up now. – simonbs Apr 10 '11 at 16:02
  • -1 without leaving a comment? I clearly stated that this should generally be done differently design-wise, but this is what the OP was looking for... – Zaky German Apr 25 '11 at 20:26
  • Heh, yeah I got a downvote without a comment as well. I upvoted yours just to "even things out". *shrug* – Kalle Apr 26 '11 at 06:38
0
-(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
  }
}
Qantas 94 Heavy
  • 15,750
  • 31
  • 68
  • 83
  • 1
    Please format your code using the `{}` toolbar button. Also, please explain how your code solves the problem. – rgettman Oct 23 '13 at 20:10
0

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.

Kalle
  • 13,186
  • 7
  • 61
  • 76