NSXMLParser on iOS8 - NSXMLParser does not support reentrant parsing

6.2k views Asked by At

I have NSXMLParser problem, and i tried iOS8 NSXMLParser crash this topic, but i really did not get the solution.

I am creating another NXSMLParser delegate and setting its delegate in another class.

Could you please tell me what to do exactly, step by step? I am so confused.

Here is my code;

These lines of codes are inside the STXMLParser

   STXMLParser2 *stXMLParser2 = [[STXMLParser2 alloc]init];    

    stXMLParser2.xmlParser = [[NSXMLParser alloc] initWithData:responseLoader.xmlData];
    [stXMLParser2.xmlParser setDelegate:self];
    [stXMLParser2.xmlParser setShouldResolveExternalEntities:YES];
    [stXMLParser2.xmlParser parse];
3

There are 3 answers

0
RawMean On

I was getting the same error and it turned out that the problem was due to calling a UI update in the func parserDidEndDocument(parser: NSXMLParser) which does not run on the main thread. After forcing the UI update in that function to run on the main queue, the problem was resolved.

2
rozochkin On

You can try this code:

dispatch_queue_t reentrantAvoidanceQueue = dispatch_queue_create("reentrantAvoidanceQueue", DISPATCH_QUEUE_SERIAL);
    dispatch_async(reentrantAvoidanceQueue, ^{
        STXMLParser2 *stXMLParser2 = [[STXMLParser2 alloc]init];    
        stXMLParser2.xmlParser = [[NSXMLParser alloc] initWithData:responseLoader.xmlData];
        [stXMLParser2.xmlParser setDelegate:self];
        [stXMLParser2.xmlParser setShouldResolveExternalEntities:YES];
        [stXMLParser2.xmlParser parse];
    });
    dispatch_sync(reentrantAvoidanceQueue, ^{ });
0
Mike Lischke On

I encountered the same problem recently but it turned out that I had an exception in one of my delegates (KVO problem) and once I fixed that the reentracy error went away. So it might be worth to look for something else if you don't have an obvious multithreading or multiinstance problem.