NSThread的XMLParser错误

时间:2011-04-08 14:11:36

标签: iphone multithreading xml-parsing main

我有一个应用程序,我通过NSXMLParser将我的XML数据加载到UITableView中。这一切都很完美。因为我想添加一个ActivityIndi​​cator,我必须将我的加载数据放在不同的主题上。在我这样做之后,XML加载以及我的应用程序,但我在表中看不到任何内容。当我单击tabbarcontroller的不同选项卡,然后单击返回表格时,表格中的数据变为可见。出了什么问题?

我的档案:

#import "DAFAppDelegate.h"
#import "RootViewController.h"
#import "XMLParser.h"


@implementation DAFAppDelegate

@synthesize window;
@synthesize navigationController;
@synthesize rootViewController;
@synthesize rootTabController;
@synthesize stages;

+ (void) showAlert
{
    UIAlertView *av = [[[UIAlertView alloc] initWithTitle:@"No Connection" message:@"Could not retrieve data" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil] autorelease];
    [av show];
}

- (void)applicationDidFinishLaunching:(UIApplication *)application
{
    [window addSubview:[rootTabController view]];
    [window makeKeyAndVisible];

    [NSThread detachNewThreadSelector:@selector(parseXML) toTarget:self withObject:nil];
}

- (void) parseXML
{
    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];

    [[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:YES];
    NSURL *url = [[NSURL alloc] initWithString:@"http://web.me.com/ijar/Stages.xml"];
    NSXMLParser *xmlParser = [[NSXMLParser alloc] initWithContentsOfURL:url];

    //Initialize the delegate.
    XMLParser *parser = [[XMLParser alloc] initXMLParser];

    //Set delegate
    [xmlParser setDelegate:parser];

    //Start parsing the XML file.
    BOOL success = [xmlParser parse];

    if(success)
    {
        NSLog(@"No Errors");
    }
    else
    {
        [DAFAppDelegate showAlert];
        NSLog(@"Error Error Error!!!");
    }  

    [pool release];

}

- (void)dealloc
{
    [navigationController release];
    [rootViewController release];
    [rootTabController release];
    [window release];
    [stages release];
    [super dealloc];
}

@end

1 个答案:

答案 0 :(得分:0)

您是否在桌面视图上致电reloadData了?您还应该使用-performSelectorOnMainThread:(SEL)aSelector withObject:(id)arg waitUntilDone:(BOOL)wait将消息发送到表格视图。

如果解析器成功,您应该发送这些消息。

if (succeed)
{
   [myTableView performSelectorOnMainThread:@selector(reloadData) withObject:nil waitUntilDone:NO];
}

最好调用表视图的数据源对象来更新它的数据存储,然后从那里告诉表视图更新其数据。