This may sound a newbie question, however I’m new to iOS dev.
I’ve a view pushed in a navigation controller, and in that view on some button click I run following code.
mUploadeImageConnection = [[NSURLConnection alloc] initWithRequest:request delegate:self startImmediately:YES];
[request release];
if (mUploadeImageConnection) {
mUploadImageResponseData = [[NSMutableData data] retain];
} else {
[mUploadeImageConnection release];
}
I’ve also implemented following methods for connection status handling
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
- (void)connectionDidFinishLoading:(NSURLConnection *)connection
When I emit mUploadeImageConnection request, it takes some time to get response. But I’m not waiting for reply and pressing the navigation back button, i.e. I’m popping the view, but still I’m getting the four connection handling functions working, although the view from where the mUploadeImageConnection request was sent is popped. In case of didFailWithError I’m showing some alertView, which will be strange in this case to show, i.e. showing alerView about some view which is already popped(is not visible/user came to previous view).
So now my questions
- What is the best practice to handle this kind of situation ?
- How can I make my four function to stop ?
If you want to stop the request, keep you NSURLConnection in a variable and call
When the user clicks the back button.
For instance, you can call the cancel in your navigation controller’s delegate, using the delegate method
by testing if the viewController that is going to be shown is your rootViewController.