Having a problem with NSURLConnection, if I create a NSURLConnection and call [connection connectionWithRequest] let it load a little then call [connection cancel] most of t
You should not release the connection & associated storage until your delegate receives either a connectionDidFinishLoading:
or a connectionDidFailWithError:
message.
Delegates are not normally retained by the object they're acting as delegate for. However in this case it is, so the delegate should not become invalid while the NSURLConnection is still referring to it, unless you're over-releasing it somehow.
I have not yet run into this problem, but this could also work without tying up your delegate object:
Since all delegate methods receive the calling Connection object as a parameter and you also know your actual active Connection object (or nil), simply ignore the delegation actions by comparing the two objects. This way a cancelled "ghost" Connection object can still call the delegate but not interfere with its internals.
- (void) connection:(NSURLConnection*) connection didReceiveData:(NSData*) data
{
if(connection != _URLConnection){return;}
...
[_incomingData appendData:data];
...
}
where _URLConnection
is a property in your delegate set to an active Connection, or nil.