Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

iOS8 NSXMLParser crash

I had a crash in NSXMLParser

* Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'NSXMLParser does not support reentrant parsing.'

Here is my code

NSString *wrappedSnippet = [NSString stringWithFormat:@"<html>%@</html>", self.snippet];
NSXMLParser *parser = [[NSXMLParser alloc] initWithData:[wrappedSnippet dataUsingEncoding:NSUTF8StringEncoding]];
[parser setDelegate:self];
[parser parse];

app crashes on the last line.

Note, that everything works perfect on iOS7!

like image 487
CrimeZone Avatar asked Aug 18 '14 12:08

CrimeZone


2 Answers

iOS8 throws an exception that previous versions caught and handled in the background.
see manual As from ios 5 NSXMLParser is thread safe but not reentrant! make sure you aren't calling parse from your NSXMLParser delegate. "Self" in your case.

like image 115
bnduati Avatar answered Oct 07 '22 17:10

bnduati


dispatch_queue_t reentrantAvoidanceQueue = dispatch_queue_create("reentrantAvoidanceQueue", DISPATCH_QUEUE_SERIAL);
    dispatch_async(reentrantAvoidanceQueue, ^{
        NSXMLParser* parser = [[NSXMLParser alloc] initWithData:xml];
        [parser setDelegate:self];
        if (![parser parse]) {
            NSLog(@"There was an error=%@ parsing the xml. with data %@", [parser parserError], [[NSString alloc] initWithData:xml encoding: NSASCIIStringEncoding]);
        }
        [parser release];
    });
    dispatch_sync(reentrantAvoidanceQueue, ^{ });

Replace your code with above lines, Hope it helps you!

like image 43
Amit Singh Avatar answered Oct 07 '22 17:10

Amit Singh