I'm having an issue where NSURLConnection seems to be causing my application to crash. If I have code something like this then my application crashes after the timeout period, always and without exception.
As far as I can see this is valid code. But 30 seconds after the start call the app is crashed every single time. I think what is happening is that I am releasing the connection object (currentConnection) far quicker than the timeout as the download completes within that time. The timeout handler that NSURLConnection has setup then fires and causes the exception.
So has anyone else seen this? If so did you come up with a good solution? The only solution I can think of is to hold on to the connection object for at least as long as timeout and then release it afterwards...
Code:
// currentConnection and downloadedData are instance variables
- (void) startDownload
{
// URL is setup and valid
NSURLRequest *request = [[NSURLRequest alloc] initWithURL:URL cachePolicy:NSURLRequestReloadIgnoringLocalCacheData timeoutInterval:30.0];
currentConnection = [[NSURLConnection alloc] initWithRequest:request delegate:self];
downloadedData = [[NSMutableData alloc] init];
[request release];
// Start the download
[currentConnection start];
}
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
// Append the received data to the current data
[downloadedData appendData:data];
}
- (void) downloadComplete
{
[downloadedData release];
downloadedData = nil;
[currentConnection release];
currentConnection = nil;
}
// parseData does exist and parses the downloaded HTML to extract data. It deals with saving the parsed data to the data model
- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
[self parseData];
[self downloadComplete];
}
As far as I can see this is valid code. But 30 seconds after the start call the app is crashed every single time. I think what is happening is that I am releasing the connection object (currentConnection) far quicker than the timeout as the download completes within that time. The timeout handler that NSURLConnection has setup then fires and causes the exception.
So has anyone else seen this? If so did you come up with a good solution? The only solution I can think of is to hold on to the connection object for at least as long as timeout and then release it afterwards...