Cesar
Cesar

Reputation: 2057

reloadData not refreshing screen

I'm a newbie iOS programmer working on my first app, and I've run into a little problem. I want to update my tableView after downloading some data over the web, and have the following code inside its controller to do so:

-(void)storeChanged:(EventsStore *)store
{
    NSLog(@"Store changed!");
    NSLog(@"tableview is %@", self.tableView);

    self.openSectionIndex = NSNotFound;

    NSLog(@"event count: %i", [[[EventsStore sharedStore] allEvents] count]);

    [self.tableView reloadData];   
}

(note: the store is a singleton so the parameter there is not needed)

when it runs I get the following output:

2015-10-19 15:12:42.487 Appname[3366:69443] Store changed!
2015-10-19 15:12:42.487 Appname[3366:69443] tableview is <UITableView: 0xc00e000; frame = (0 0; 320 480); clipsToBounds = YES; autoresize = RM+BM; gestureRecognizers = <NSArray: 0xb114d30>; layer = <CALayer: 0xb112f30>; contentOffset: {0, -64}; contentSize: {320, 0}>
2015-10-19 15:12:42.487 Appname[3366:69443] event count: 12

But the tableView remains empty until I try to scroll, at which point it displays everything correctly.

The tableViewController is a custom class (extends UITableViewController), displayed inside a navigationController, both of which are inside of a storyboard. As far as I can figure out all the outlets have been set, but if you have an idea of how to possibly troubleshoot a faulty outlet let me know.

Edit: I just had an idea what the cause could be: This function is called from inside a completionBlock. If that is the reason, does anyone know how to circumvent it?

Upvotes: 1

Views: 46

Answers (1)

Michael Dautermann
Michael Dautermann

Reputation: 89509

You're most likely doing storeChanged on a non-main thread.

Do this and you will be happy:

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

Upvotes: 2

Related Questions