dispatch_async与xml解析器不工作

时间:2016-04-29 12:36:36

标签: ios xml uitableview dispatch-async

我正在使用dispatch_async在一个合适的视图中解析xml数据这里是我的代码:

- (void) Parse{
    dispatch_async( dispatch_get_global_queue(0, 0), ^{
     NSString *post =[[NSString alloc] initWithFormat:@"http://messages.xml"];
     NSData *xmlData=[[NSData alloc]initWithContentsOfURL:[NSURL URLWithString:post]];
     xmlParserObject =[[NSXMLParser alloc]initWithData:xmlData];
     [xmlParserObject setDelegate:self];
     dispatch_async( dispatch_get_main_queue(), ^{
     [xmlParserObject parse];
     });
     });
    [messageList reloadData];
}

现在uitableview没有显示任何数据。它在dispatch_async

之前完美运行

1 个答案:

答案 0 :(得分:1)

我认为您的代码存在问题。请在dispatch_get_main_queue()

中重新加载表格数据
- (void) Parse {
    dispatch_async( dispatch_get_global_queue(0, 0), ^{

        NSString *post =[[NSString alloc] initWithFormat:@"http://messages.xml"];
        NSData *xmlData=[[NSData alloc]initWithContentsOfURL:[NSURL URLWithString:post]];
        xmlParserObject =[[NSXMLParser alloc]initWithData:xmlData];
        [xmlParserObject setDelegate:self];
        [xmlParserObject parse];
        dispatch_async( dispatch_get_main_queue(), ^{
            [messageList reloadData];
        });
    });    
}