IOS8 Nsxmlparser does not support reentrant parsing error problem resolution

Source: Internet
Author: User

One day, in the ios8.1 run a previously no problem of the old project, log in time appeared: Nsxmlparser does not support reentrant parsing.

Direct translation into Chinese, the system's XML parsing does not support foldback parsing, do not understand! Then began to write the demo test problem.

Because this project login interface merges 2 different uses of data into the same interface, the code parses the XML data 2 times, so it first simulates multiple parsing of a piece of data

1, Demo1, in the same function, call 2 times XML parsing code

    NSString *xmlstr = @ "<test><goods>asdf</goods><goods>asdf</goods></test>";    NSData *xmldata = [Xmlstr datausingencoding:nsutf8stringencoding allowlossyconversion:yes];    Nsxmlparser * parser = [[Nsxmlparser alloc] initwithdata:xmldata];    Parser.delegate = self;    [parser parse];    Nsxmlparser * Parser1 = [[Nsxmlparser alloc] initwithdata:xmldata];    Parser1.delegate = self;    [Parser1 parse];

Run after the discovery of normal operation, no error occurred. Re-study the code of the old project, found that the old project in the first time the XML data is successfully logged in the success of a parse, and in the block after the successful parsing, another parse to obtain another data, then the simulation

2, Demo2, in the XML parsing of the delegate callback, then the XML parsing

-(void) btnpublishonclick{nsstring *xmlstr = @ "<test><goods>asdf    </goods><goods>asdf</goods></test> ";    NSData *xmldata = [Xmlstr datausingencoding:nsutf8stringencoding allowlossyconversion:yes];    Nsxmlparser * parser = [[Nsxmlparser alloc] initwithdata:xmldata];    Parser.delegate = self;    Self.curparsertype = @ "First";    [Parser parse]; return;} -(void) Parserdidenddocument: (Nsxmlparser *) parser{nsstring *xmlstr = @ "<test><goods>asdf</goods>    <goods>asdf</goods></test> ";    NSData *xmldata = [Xmlstr datausingencoding:nsutf8stringencoding allowlossyconversion:yes]; if ([Self.curparsertype isequaltostring:@ "First"]) {Nsxmlparser * parser1 = [[Nsxmlparser alloc] Initwithdata:xmld        ATA];        Self.curparsertype = @ "Second";        Parser1.delegate = self;    [Parser1 parse]; }}

Sure enough, after the run, appear nsxmlparser does not the support Reentrant parsing error, then you can draw the following conclusions:

In the XML parsing delegate callback (for example: parserdidenddocument), two times the XML parsing, there will be nsxmlparser does not the support Reentrant parsing error

Workaround:

1, the thread, two times to parse the XML code into the thread execution

-(void) Parserdidenddocument: (Nsxmlparser *) parser{    nsstring *xmlstr = @ "<test><goods>asdf</ Goods><goods>asdf</goods></test> ";    NSData *xmldata = [Xmlstr datausingencoding:nsutf8stringencoding allowlossyconversion:yes];    if ([Self.curparsertype isequaltostring:@ "First"]) {        dispatch_queue_t reentrantavoidancequeue = Dispatch_queue_ Create ("Reentrantavoidancequeue", dispatch_queue_serial);        Dispatch_async (Reentrantavoidancequeue, ^{            nsxmlparser * parser1 = [[Nsxmlparser alloc] initwithdata:xmldata];            Self.curparsertype = @ "Second";            Parser1.delegate = self;            [Parser1 parse];        });        Dispatch_sync (Reentrantavoidancequeue, ^{});}    }

2. Delay execution of code parsing XML two times using Performselector

-(void) Sencondparser: (NSData *) xmlData {    Nsxmlparser * parser1 = [[Nsxmlparser alloc] initwithdata:xmldata];    Self.curparsertype = @ "Second";    Parser1.delegate = self;    [Parser1 parse];} -(void) Parserdidenddocument: (Nsxmlparser *) parser{    nsstring *xmlstr = @ "<test><goods>asdf</ Goods><goods>asdf</goods></test> ";    NSData *xmldata = [Xmlstr datausingencoding:nsutf8stringencoding allowlossyconversion:yes];    if ([Self.curparsertype isequaltostring:@ "First"]) {        [self performselector: @selector (sencondparser:) withobject : XmlData afterdelay:0.1];}    }

IOS8 Nsxmlparser does not support reentrant parsing error problem resolution

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.