Samhan Salahuddin
Samhan Salahuddin

Reputation: 2170

Loading data into UITableview using GCD

The code below loads data into a UITableView by getting a google news RSS feed parsing the XML and putting it into the view . It works but when i push another view and come back the table view scroll is broken . I have isolated the problem to the GCD code . If i remove it the problem disappears . So here is the GCD code :

- (void)viewDidLoad
{
    [super viewDidLoad];

    NSLog(@"View did load ..");
    self.title = @"News stories";

    NewsItem *item = [[NewsItem alloc] init];
    item.title = @"Loading ...";

    self.newsItems = [@[item] mutableCopy];
    NSString *URL = @"http://news.google.com/news?q=apple+OR+google+OR+microsoft&output=rss";
    NSURL *xmlURL = [NSURL URLWithString:URL];
    NSURLRequest *request = [NSURLRequest requestWithURL:xmlURL];
    Parser *parser =  [[Parser alloc] initXMLParser];;

    dispatch_queue_t downloadQueue = dispatch_queue_create("news downloader", NULL);

    dispatch_async(downloadQueue, ^{
        BOOL success;
        NSData *data = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];

        if (data != nil) {

            NSXMLParser *nsXmlParser = [[NSXMLParser alloc] initWithData:data];

        // create and init our delegate


        // set delegate
            [nsXmlParser setDelegate:parser];

        // parsing...
            success = [nsXmlParser parse];
        }

        else {
            success = FALSE;
        }
        // test the result
        dispatch_async(dispatch_get_main_queue(), ^{
        if (success) {

                NSLog(@"reloading data ...");
                self.newsItems = [parser.newsItems copy];
                [self.tableView reloadData];

        } else {
                 NewsItem *item = [[NewsItem alloc] init];
                item.title = @"Error loading";

                self.newsItems = [@[item] mutableCopy];
                [self.tableView reloadData];


            NSLog(@"Error parsing document!");
        }

        });


    });



}

Upvotes: 1

Views: 493

Answers (1)

Mundi
Mundi

Reputation: 80265

Try [self.tableView reloadData]; in viewDidAppear.

Upvotes: 2

Related Questions