码迷,mamicode.com
首页 > 移动开发 > 详细

IOS8下NSXMLParser does not support reentrant parsing报错问题解决

时间:2015-01-08 11:06:57      阅读:413      评论:0      收藏:0      [点我收藏+]

标签:

某天,在ios8.1下运行了一个之前没有问题的老项目,登录的时候出现了:NSXMLParser does not support reentrant parsing。

直接翻译成中文,系统的xml解析不支持折返解析,不理解!于是开始写demo测试问题。

因为此项目登录接口将2份用途不同的数据合并在同一个接口返回,代码中对此xml数据解析了2次,所以首先模拟对一份数据进行多次解析的情况

1、demo1,在同一个函数中,调用2次xml解析代码

    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后发现运行正常,并未出现报错。再研究下老项目的代码,发现老项目中首先对xml数据是否登录成功进行了一次解析,并在解析成功后调用的block中,再进行了另一次解析获取另一份数据,那么模拟之

2、demo2,在xml解析的delegate回调中,再进行xml解析

- (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:xmlData];
        self.curParserType = @"second";
        parser1.delegate = self;
        [parser1 parse];
    }
}

 果然,运行后,出现NSXMLParser does not support reentrant parsing的报错,那么可以得出以下结论:

在xml解析的delegate回调中(如:parserDidEndDocument),进行二次xml解析,就会出现NSXMLParser does not support reentrant parsing的报错

解决办法:

1、开线程,将二次解析xml的代码放在线程中执行

- (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、使用performSelector,将二次解析xml的代码延迟执行

- (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报错问题解决

标签:

原文地址:http://www.cnblogs.com/rooney-10/p/4210216.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!