views:

19

answers:

1

Hello,

I'm trying to do two NSURLRequests after each other. The second request is based on the results from the first one. My first request is working fine. I have set the delegate to self and I'm informed when when the request is finished (in the connectionDidFinishLoading method).

My question is now, how can I do the second request and be informed when it's done? Do I have to create a second delegate which handles my request or is there any other/better way to handle this?

Thanks for help.

+1  A: 

What I've done in the past is wrap the code for the NSURLConnection's delegate in a concurrent NSOperation class. This abstract class takes care of all the work in setting up and managing the connection and its state, and subclasses of it perform the actual work on the data when the connection finishes. For example, the superclass calls a method performWork:(NSData *)data that is overridden by the subclasses, and sets an instance variable for the result. You can use a controller class to manage the objects in an NSOperationQueue, and create new operations when previous operations finish. In this way each request is a single manageable unit, requests don't necessarily need to know about each other, and you aren't copy and pasting code to manage each connection.

Marc Charbonneau