Fogmeister
Fogmeister

Reputation: 77641

NSURLConnectionDataDelegate order of functions

In the NSURLConnectionDataDelegate there are a couple of functions that are pretty essential to making sure everything worked but I'm never sure what happens when.

The functions...

- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response;
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data;
- (void)connectionDidFinishLoading:(NSURLConnection *)connection;

and a couple of others.

Do they always happen in the order I've put them? i.e. is the response the first thing you get or can it happen any time in the life of the connection?

Upvotes: 0

Views: 920

Answers (2)

Jason Coco
Jason Coco

Reputation: 78363

-connection:didReceiveResponse: will be called 0 or more times. If there's an error, -connection:didFailWithError: will be called instead. This method may be called more than once if you're getting a multi-part mime message and will be called once there's enough date to create the response object and before -connection:didReceiveData: is called.

-connection:didReceiveData: will be called 0 or more times. If there is more than a 0 byte body, this method will be called at least once before -connection:didFinishLoading: is called. This method will never be called before -connection:didReceiveResponse: or after -connection:didFinishLoading: or -connection:didFailWithError:.

-connection:didFinishLoading: is called only once and it's the last thing called. Sometime after this method returns, the connection will be released. This method isn't called if -connection:didFailWithError: is called and is always the last thing called.

The documentation for when these methods are called and in which sequence exist in the header files, but I haven't seen it written up super concisely in the actual docs.

Upvotes: 4

V-Xtreme
V-Xtreme

Reputation: 7333

For the delegate methods no matter in which order order do you put them . They will occur when the particular event is met .

The second thing, as per the document the NSURLConnectionDataDelegate Protocol Reference :

didReceiveResponse: This method is called when the server has determined that it has enough information to create the NSURLResponse. It can be called multiple times, for example in the case of a redirect, so each time we reset the data.

didReceiveData: This method is called when there is newly available data from the connection. This method is called multiple time

connectionDidFinishLoading:Sent when a connection has finished loading successfully. This method also called multiple time in case of redirect .

Upvotes: 2

Related Questions