NSURLConnection and processing of response code and response data

I have a case with an odd edge right now in that response code from the delegate method of NSURLConnection:

- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response; 

Run before the next delegation method:

 - (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data; 

My code, of course, can use the enhancement, as right now it checks the HTTP response code from the above, and then calls some method to take action. Unfortunately, data is not yet available at this point.

What are some elegant solutions for connecting response and responseData in such a way that my class method does not start until response and responseData become 200 + nil . Do I need to set both of them as class instance variables? Sounds like a poor man's decision.

+3
source share
1 answer

The answer to the data is the correct order. In fact, you should clear any data in this method (if you get multiple responses through redirects and any intermediate data is out of date).

You get a connection: didReceiveResponse: message to tell you that the response header is received, but this happens before any body content.

If you need access to all response elements, you just need to save the response and data as they become available and only process them in connectionDidFinishLoading: (or if your data is long, you can process it step by step in the connection: didReceiveData :.)

+8
source

All Articles