NSURLConnection and handling response code and response data

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

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

Is triggered before the subsequent delegate method:

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

My code could certainly use improvement as right now it checks for the HTTP Response Code from the above, then calls on some method to take action. Unfortunately at that point, the data is not yet available.

What are some elegant solutions for coupling the response and the responseData in a fashion that my classes method is not triggered until the response and the responseData are 200 + not nil. Do I need to set both of them as class instance variables? Seems like a poor man's solution.

Answers


Response before data is the correct order. In fact, you should be clearing any data in this method (in case you receive multiple responses through redirections and any intervening data is made obsolete).

You receive a connection:didReceiveResponse: message to tell you that a reply header has been received but this occurs before any body content.

If you need access to all elements of the reply, you should simply store the response and the data as they come in and only handle them in connectionDidFinishLoading: (or if your data is long, you can handle incrementally in connection:didReceiveData:).


Need Your Help

Page Cannot Be Liked with Facebook Like Button

javascript html facebook facebook-like

On http://www.btandthetenants.com, every page can be liked except for the Photos section. I use AJAX to change the current page's like button and the script performs perfectly on every page includ...

How to get RibbonApplicationMenuItem's Click event

c# wpf xaml ribbon

I'm using Microsoft Ribbon, and I'm trying to get it working.