iPhone:在后台解析多个XML用的NSXMLParser互不干扰 [英] iPhone: Parsing multiple XML with NSXMLParser in background disturbing each other

查看:107
本文介绍了iPhone:在后台解析多个XML用的NSXMLParser互不干扰的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个奇怪的问题,当谈到与的NSXMLParser iPhone上解析XML。当启动应用程序,我想preLOAD 4表视图,由填充的RSS源的背景。

I have a strange issue, when it comes to parsing XML with NSXMLParser on the iPhone. When starting the app, I want to preload 4 table-views, that are populated by RSS-Feeds in the background.

在我的初始化表视图中的一个接一个,比加载,解析和显示像一个魅力的所有的作品。但是,当我尝试一次初始化所有视图(在同一时间),似乎比,即 XML解析器-实例互不干扰的。不知何故,从一个XML的饲料数据,广播到其他的XML解析器的情况下,他们不属于。例如:有一个团队成员项目,以这是我的名字。当这个错误发生时,有一个从添加了另一个XML的饲料,即产生一个字符串:这是我name58,其中58是从另一种观点的东西图表位置。 58似乎再错过另一实例。

When I init the table-views one-by-one, than loading, parsing and displaying all works like a charm. But when I try to init all view at once (at the same time), than it seems, that the XML-parser-instances are disturbing each other. Somehow data from one XML-Feed are "broadcasted" into other xml-parser instances, where they do not belong. Example: there is a "teammember" item, with "This is my name". When this bug occurs, there is a string from another xml-feed added, i.e. resulting in: "This is my name58", where 58 is the chart-position of something from the other view. "58" seems to miss then on the other instance.

在我看来,这个错误发生的原因的NSXMLParser,委托方法:

It looks to me, that this bug occurs because of the NSXMLParser-delegate method:

- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string {
    if (!currentStringValue) {
        currentStringValue = [[NSMutableString alloc] initWithCapacity:50];
    }
    [currentStringValue appendString:string];   
}

在这种情况下,巧合字节附加到字符串,在那里他们不属于

In this case "by coincidence" bytes are appended to strings, where they do not belong to.

但奇怪的是,的NSXMLParser的每个实例都是独一无二的,有自己独特的代表,附加到自己的ViewController。每个解析,请求派生它自己的后台任务,有自己的(也也是独一无二的名字命名)自动释放池。

The strange thing is, that every instance of NSXMLParser is unique, got its own unique delegates, that are attached to their own ViewController. Every parsing-requests spawns it own background-task, with its own (also also unique named) Autorelease-pool.

我打电话的的NSXMLParser这样在ViewController中:

I am calling the NSXMLParser like this in the ViewController:

// prepare XML saving and parsing
currentStringValue = [[[NSMutableString alloc] initWithCapacity:50] retain];    
charts = [[NSMutableArray alloc] init];

NSURL *url = [[NSURL alloc] initWithString:@"http://(SOME XML URL)"];
xmlParser = [[[NSXMLParser alloc] initWithContentsOfURL:url] retain];

//Set delegate
[xmlParser setDelegate:self];

//loading indicator 
progressWheel = [[[UIActivityIndicatorView alloc] initWithFrame:CGRectMake(150.0,170.0,20.0,20.0)] autorelease];
progressWheel.activityIndicatorViewStyle = UIActivityIndicatorViewStyleGray;

[self.view addSubview:progressWheel];
[progressWheel startAnimating];

// start loading and parsing the xml-feed in the background
//[self performSelectorInBackground:@selector(parse:) withObject:xmlParser]; -> I also tried this
[NSThread detachNewThreadSelector:@selector(parse:) toTarget:self withObject:xmlParser];

这是背景,任务之一,解析饲料:

And this is one of the background-tasks, parsing the feed:

- (无效)解析:(*的NSXMLParser)myParser {

-(void)parse:(NSXMLParser*)myParser {

NSAutoreleasePool *schedulePool = [[NSAutoreleasePool alloc] init];

BOOL success = [myParser parse];

if(success) {
    NSLog(@"No Errors. xmlParser got: %@", myParser);

    (POST-PROCESSING DETAILS OF THE DATA RETURNED)

    [self.tableView reloadData];            

} else {    
    NSLog(@"Couldn't initalize XMLparser");
}

[progressWheel stopAnimating];

[schedulePool drain];

[myParser release]; 

}

这是什么原因这个问题?难道我打电话以正确的方式在后台任务?为什么这个bug接近,因为每个XML解析器有自己的独特的实例?

What could cause this issue? Am I calling the background-task in the right way? Why is this bug approaching, since every XML-Parser got its own, unique instance?

推荐答案

您不应该更新从后台线程中的UI元素(如 progressWheel )。 UI更新应该在主线程中完成。

You should not be updating UI elements (like progressWheel) from inside a background thread. UI updates should be done on the main thread.

使用 -performSelectorOnMainThread:withObject:waitUntilDone:来从后台线程中更新UI元素

Use -performSelectorOnMainThread:withObject:waitUntilDone: to update UI elements from within a background thread.

这篇关于iPhone:在后台解析多个XML用的NSXMLParser互不干扰的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆