NSThread 的 XMLParser 错误

发布于 2024-10-31 14:31:00 字数 1885 浏览 0 评论 0原文

我有一个应用程序,在其中我通过 NSXMLParser 将 XML 数据加载到 UITableView 中。这一切都很完美。因为我想添加一个 ActivityIndi​​cator,所以我必须将加载数据放在与主线程不同的线程上。执行此操作后,XML 以及我的应用程序都被加载,但我在表中看不到任何内容。当我单击选项卡控制器的不同选项卡,然后单击返回表时,表中的数据变得可见。出了什么问题?

我的文件:

#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

I have an application in which I load my XML data via the NSXMLParser into an UITableView. This all worked perfectly. Because I wanted to add a ActivityIndicator I have to place my loading data on a different thread then the main. After I did this the XML get loaded as well as my application but I see nothing in my table. When I click on a different tab of my tabbarcontroller and then click back to the table the data in the table becomes visible. What is going wrong?

My file:

#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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

歌枕肩 2024-11-07 14:31:00

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

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

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

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

Did you call reloadData on your table view? Also you should send the message to the table view using -performSelectorOnMainThread:(SEL)aSelector withObject:(id)arg waitUntilDone:(BOOL)wait.

You should send these messages if your parser succeeded.

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

It might be better to call your table view's datasource object to update it's data store and then from there, tell the table view to update its data.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文