Multiple asynchronous URL requests
Asked Answered
M

3

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?

Mouser answered 10/4, 2011 at 10:34 Comment(1)
This is incredibly easy to do with ASIHttpRequest which is the most used 3rd party library in all of iPhone. Your problem is solved.Aubervilliers
D
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
    }
}
Dexter answered 10/4, 2011 at 11:40 Comment(5)
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?Mouser
@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 answerDexter
Of course! Thank you very much. This solved my issue. My application does not "freeze" on start up now.Mouser
-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...Dexter
Heh, yeah I got a downvote without a comment as well. I upvoted yours just to "even things out". shrugTeriann
T
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.

Teriann answered 10/4, 2011 at 10:41 Comment(0)
S
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
  }
}
Shoup answered 23/10, 2013 at 19:44 Comment(1)
Please format your code using the {} toolbar button. Also, please explain how your code solves the problem.Flanker

© 2022 - 2024 — McMap. All rights reserved.