开发者

RSS on a different thread doesnt work but works fine when on the main thread

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 开发者_StackOverflow社区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];
}


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜