不同线程上的 RSS 不起作用,但在主线程上工作正常

发布于 2024-10-03 12:57:51 字数 835 浏览 5 评论 0原文

我试图在我的 iPhone 应用程序中的不同线程上获取 rss 解析器,但是当我这样做时,我只得到旋转指示器(即什么也没有)。但是,如果我注释掉 viewDidAppear 中的调用 [NSThread....],并取消注释行 [self loadData],则一切正常(但它不在不同的线程上)。我错过了什么吗?感谢您可以在这里提供的任何见解!

这是代码。

- (void)viewDidAppear:(BOOL)animated {

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

              //[self loadData];

    [super viewDidAppear:animated];
}



- (void)loadData {

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

    if (items == nil) {
        [activityIndicator startAnimating];

        Parser *rssParser = [[Parser alloc] init];
        [rssParser parseRssFeed:@"http://www.mywebsite.com/xml" withDelegate:self];

        [rssParser release];

    } else {
        [self.tableView reloadData];
    }
        [pool release];
}

I'm trying to get an rss parser on a different thread in my iphone app, but when I do this I only get the spinning indicator (i.e., nothing). But if I comment out the call [NSThread....] in viewDidAppear, and uncomment the line [self loadData], everything works (but then its not on a different thread). Am I missing something? Thanks for any insight you can provide here!!

Here is the code.

- (void)viewDidAppear:(BOOL)animated {

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

              //[self loadData];

    [super viewDidAppear:animated];
}



- (void)loadData {

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

    if (items == nil) {
        [activityIndicator startAnimating];

        Parser *rssParser = [[Parser alloc] init];
        [rssParser parseRssFeed:@"http://www.mywebsite.com/xml" withDelegate:self];

        [rssParser release];

    } else {
        [self.tableView reloadData];
    }
        [pool release];
}

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

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

发布评论

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

评论(1

梓梦 2024-10-10 12:57:51

所有 UI 更改都应在主线程上进行:

- (void)viewDidAppear:(BOOL)animated {

    if (items == nil)
    {
        [activityIndicator startAnimating];
        [NSThread detachNewThreadSelector:@selector(loadData) toTarget:self withObject:nil];
    }
    else
    {
        [self.tableView reloadData];
    }
    [super viewDidAppear:animated];
}

- (void)loadData {

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

        Parser *rssParser = [[Parser alloc] init];
        [rssParser parseRssFeed:@"http://www.mywebsite.com/xml" withDelegate:self];
        [rssParser release];

        [pool release];
}

检查 items 是否为 null,如果是,则开始为指示器设置动画,然后然后启动新线程。

All UI changes should be made on the main thread:

- (void)viewDidAppear:(BOOL)animated {

    if (items == nil)
    {
        [activityIndicator startAnimating];
        [NSThread detachNewThreadSelector:@selector(loadData) toTarget:self withObject:nil];
    }
    else
    {
        [self.tableView reloadData];
    }
    [super viewDidAppear:animated];
}

- (void)loadData {

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

        Parser *rssParser = [[Parser alloc] init];
        [rssParser parseRssFeed:@"http://www.mywebsite.com/xml" withDelegate:self];
        [rssParser release];

        [pool release];
}

Check if items is null, if it is, start animating the indicator and then start the new thread.

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