4

I have a UITableViewController, which is loading data from a NSURLConnection:

NSURL *url = [NSURL URLWithString:@"http://url.now"];
NSURLRequest *request = [NSURLRequest requestWithURL:url cachePolicy:NSURLRequestReloadIgnoringCacheData timeoutInterval:30.0];

NSOperationQueue *queue = [[NSOperationQueue alloc] init];

[NSURLConnection sendAsynchronousRequest:request queue:queue completionHandler:^(NSURLResponse *response, NSData *data, NSError *error){
    if ([data length] > 0 && error == nil){
        NSXMLParser *parser = [[NSXMLParser alloc] initWithData:data];
        [parser setDelegate:self];
        [parser parse];
    }
    else if (error != nil){
        UIAlertView* alertView = [[UIAlertView alloc] initWithTitle:@"No internet connection" message:@"Content." delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
        [alertView performSelectorOnMainThread:@selector(show) withObject:nil waitUntilDone:true];
    }
}];

When the parsing ends, the reloaddata method is called. But when the app is loaded, the content of the UITableView is not showing. When I scroll a bit, it get's visible immediately. Reloading is no problem.

How can this be fixed?

Matthias_164
  • 191
  • 1
  • 8

3 Answers3

8

Solved it. Changed the tableView reloadData to this:

 dispatch_async(dispatch_get_main_queue(), ^{
        [self.tableView reloadData];
 });

And it worked like a charm. Thanks for all answers and comments.

Matthias_164
  • 191
  • 1
  • 8
0

What probably happened is that your table loads faster than the time it takes for your commotion to download data. Add a delegate to the class that manage your data and make your table view controller conform to that delegate protocol. Than, once data is downloaded and parsed reload the table.

Michal Shatz
  • 1,416
  • 2
  • 20
  • 31
  • I don't think so, because the OP says "When the parsing ends, the reloaddata method is called." – Aaron Brager Dec 14 '13 at 22:27
  • OP? Anyway put some break points out there and see what happened first and if you have data the first time reusable cells deques. – Michal Shatz Dec 15 '13 at 09:10
  • @MichalShatz The table is indeed loaded first, but when it's loaded, tableView reloadData is called, but nothing happens. Only when the view is scrolled a bit, everything will become visible. – Matthias_164 Dec 15 '13 at 18:53
  • Reson is that when you call reloadData dat is not available yet. That's why your solution of dispatchingMainQueue "works like a charm" - you are reloading data while using the main queue instead of background and force slow load on the main queue which cause bad user experience. – Michal Shatz Dec 16 '13 at 21:56
  • 2
    Actually calling the mainQueue works because the parse was performed on the background asyc of NSURL and according to apple updating the view should be done in the mainThread thats were the mainQueue comes in it runs the the tableview reload on the main thread. – Joshua Jan 18 '14 at 17:50
0

Unchecking "Use Size Classes" option and then coming back and re-checking it again fixes this issue.

  • In the project navigator, select a storyboard or xib file.
  • The file’s contents open in Interface Builder.
  • Choose View > Utilities > Show File Inspector.
  • In the Interface Builder Document section, un-tick the "Use Size Classes" checkbox.
  • Leave the menu. Come back and tick the "Use Size Classes" checkbox again.
Fernando Rocha
  • 2,416
  • 3
  • 15
  • 28