Okay so I am having some issues when doing some NSXMLParsing. The parsers are working fine individually, but when I try to run them asynchronously in the background, only one of them will indicate that it has completed parsing the document.
To explain my full process, it goes as follows
- I load 2 webpages using multiple NSURLConnections and NSURLRequests, in the background as to not block the user interface
- I then take that data and pass it into an xml parser.
- There is a separate XML parser for each webpage, each with its own reference and memory allocated to it.
- I then run through the parser as you would normally do, and it looks for specific tags I indicate, yadda yadda, that stuff is all working fine.
- The problem comes in when I have the parsers indicate that they have finished, I use the
parserDidEndDocument:(NSXMLParser *)parsermethod to check if the document has finished or not. Unfortunately this method is only called once, so I do not know if both parsers have completed or not.
My code is this:
When the connections complete
-(void) connectionDidFinishLoading:(NSURLConnection *)connection {
if (connection == actConnection) {
[self processSynopsis]; // initiate synopsis parser
} else if (connection == castConnection) {
[self processCast]; // initiate cast parser
}
}
Allocating and starting synopsis parser
-(void) processSynopsis {
actParser = [[NSXMLParser alloc] initWithData:actData];
[actParser setDelegate:self];
[actParser parse];
}
Allocating and starting cast parser
-(void) processCast {
castParser = [[NSXMLParser alloc] initWithData:castData];
[castParser setDelegate:self];
[castParser parse];
}
Checking if the parser terminated
-(void) parserDidEndDocument:(NSXMLParser *)parser {
if (parser == actParser) {
NSLog(@"ended act %@",parser);
} else if (parser == castParser) {
NSLog(@"ended cast %@",parser);
}
}
The parserDidEndDocument method will only be called once for some reason. Anyone see anything wrong or have had this problem before. Thanks for any help in advance!
NSLog is a simple front end to NSLogv, which does the actual logging work. According to the documentation:
I’m not sure how to interpret “can begin”. This could mean that an attempt to log something while NSLogv is running will be ignored. Therefore if your second parser finishes very soon after the first parser the log message will not be sent.
Try the @synchronized() directive to prevent the two threads from stepping on each other. Something like this: