This piece of code:
- (IBAction) getXML {
goButton.enabled = NO;
[self performSelectorInBackground:@selector(parseInBackground) withObject:nil];
}
- (void)parseInBackground {
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
xmlParser = [[XMLParser alloc] init];
NSURL *xmlurl = [[NSURL alloc] initWithString:@"http://www.mysite.com/myfile.xml"];
[xmlParser fetchXMLFromURL:xmlurl];
[self performSelectorOnMainThread:@selector(didFinishXMLParsing) withObject:nil waitUntilDone:YES];
[xmlurl release];
[pool drain];
}
- (void)didFinishXMLParsing {
goButton.enabled = YES;
}
triggers this code:
- (void)fetchXMLFromURL:(NSURL *)xmlurl {
XMLData = [[NSMutableData alloc] init];
NSURLRequest *request = [[NSURLRequest alloc] initWithURL:xmlurl];
NSURLConnection *connection = [[NSURLConnection alloc] initWithRequest:request delegate:self];
[connection release];
[request release];
[[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:YES];
}
However, when I step through it in debug, as soon as it gets to '}
' in fetchXMLFromURL it returns to the line:
[self performSelectorOnMainThread:@selector(didFinishXMLParsing) withObject:nil waitUntilDone:YES];
and the connection to the URL which fetches the XML never actually gets triggered. Anyone any idea why?
This revised version seems to be working correctly, can anyone spot any potential issues?
- (void)fetchXMLFromURL:(NSURL *)xmlurl {
XMLData = [[NSMutableData alloc] init];
[[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:YES];
XMLData = [NSData dataWithContentsOfURL:xmlurl];
[[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:NO];
[self startParsingXML];
}
- (void) startParsingXML
{
NSXMLParser *xmlParser = [[NSXMLParser alloc] initWithData:XMLData];
xmlParser.delegate = self;
[xmlParser parse];
[xmlParser release];
}
Revised again, hopefully correct now
- (void)fetchXMLFromURL:(NSURL *)xmlurl {
[[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:YES];
XMLData = [[NSData alloc] initWithContentsOfURL:xmlurl];
[[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:NO];
[self startParsingXML];
}
- (void) startParsingXML
{
NSXMLParser *xmlParser = [[NSXMLParser alloc] initWithData:XMLData];
xmlParser.delegate = self;
[xmlParser parse];
[xmlParser release];
}