I need to cancel a NSMutableRequest and a XMLParsing if the user choose another view
I do this on ViewDidLoad:
NSMutableURLRequest * req = [[NSMutableURLRequest alloc]initWithURL:[NSURL URLWithString:endereco]
cachePolicy:NSURLRequestReloadIgnoringCacheData timeoutInterval:20.0f];
conn = [NSURLConnection connectionWithRequest:req delegate:self];
[UIApplication sharedApplication].networkActivityIndicatorVisible = YES;
if(conn)
{
receivedData = [[NSMutableData alloc]init];
[DSBezelActivityView newActivityViewForView:self.view withLabel:@"Obtendo Lista..."];
}
I know i have to : [conn cancel]; on viewWillDisappear, but how i can check i the connection is ocurring so i can cancel it?
And the same happens to the parse method :
-(void)connectionDidFinishLoading:(NSURLConnection *)connection
{
[UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
NSXMLParser * parser = [[NSXMLParser alloc]initWithData:receivedData];
[parser setDelegate:self];
[parser parse];
[parser release];
[receivedData release];
}
You can achieve this by using two flags(
BOOLvariables). One flag will keep track of connection activity(whether connection is still in progress or hasfinishedLoading).So, for connection (
BOOL isConnectionActive)set
isConnectionActive = YESinviewDidLoadand set it
isConnectionActive = NOinconnectionDidFinishLoadingorconnectionDidFailWithErrordelegate methods.and check
in
viewWillApear: method.similar thing you can do for xmlParsing. you will set
isParsingCompleted=NO- (void)parserDidStartDocument:(NSXMLParser *)parsermethod.and
isParsingCompleted=YESmethods and you will use
in
viewWillApear:method.[for this you will need a class level reference for parser.]Thanks,