David Findlay
David Findlay

Reputation: 1356

Updating data in UITableView after asynchronous load

I'm loading some data from a web service that I want use in a UITableView. I can set some initial values for the UITableView. However if I update those details the completionHandler block, the update doesn't occur.

- (void)viewDidLoad {
    [super viewDidLoad];

    // Do any additional setup after loading the view, typically from a nib.

    NSURLSessionConfiguration *configuration = [NSURLSessionConfiguration defaultSessionConfiguration];
    AFURLSessionManager *manager = [[AFURLSessionManager alloc] initWithSessionConfiguration:configuration];
    manager.responseSerializer = [AFHTTPResponseSerializer serializer];

    NSURL *URL = [NSURL URLWithString:@"http://www.bom.gov.au/cgi-bin/wrap_fwo.pl?IDQ60359.html"];
    NSURLRequest *request = [NSURLRequest requestWithURL:URL];

    NSURLSessionDataTask *dataTask = [manager dataTaskWithRequest:request completionHandler:^(NSURLResponse *response, id responseObject, NSError *error) {
        if (error) {
            NSLog(@"Error: %@", error);
        } else {

            // Code that passes my web service data into an NSArray list

            // self->sensors = locations;
            self->sensors = [NSArray arrayWithObjects:@"key4",@"key5",@"key6",nil];

            NSLog(@"Try to reload the tableView");
            [self.tableView reloadData];

        }
    }];
    [dataTask resume];

    self->sensors = [NSArray arrayWithObjects:@"key1",@"key2",@"key3",nil];

}

My app starts, and shows key1, key2 and key3 in the UITableView list. Key4, key5, key6 never show. The code in the completionHandler does run, I've verified it by NSLogging the way I create my array to put into the UITableView.

"Try to reload the tableView" is displayed in the console. However some NSLogs I've added to my numberOfRowsInSection and cellForRowAtIndexPath functions are not shown after "try to reload the tableView".

How can I get my code to update the UITableView to show key4, key5, key6?

Upvotes: 0

Views: 66

Answers (2)

David Findlay
David Findlay

Reputation: 1356

It turned out my problem wasn't about the thread. The code in the question works, once I added this referencing outlet:

In the storyboard

Control dragging from the UITableView in storyboard to the ViewController Interface added this line:

@property (weak, nonatomic) IBOutlet UITableView *tableView;

I had previously manually put this in but that didn't add the Referencing Outlet in the storyboard. I forgot this step. Once this step was done, I didn't have to worry about what thread I was calling UITableView reloadData from.

Upvotes: 0

Jen Jose
Jen Jose

Reputation: 4045

Try reloading tableview on main thread

- (void)viewDidLoad {
    [super viewDidLoad];

    // Do any additional setup after loading the view, typically from a nib.
    self->sensors = [[NSMutableArray alloc] initWithObjects: @"key1",@"key2",@"key3",nil];  

    NSURLSessionConfiguration *configuration = [NSURLSessionConfiguration defaultSessionConfiguration];
    AFURLSessionManager *manager = [[AFURLSessionManager alloc] initWithSessionConfiguration:configuration];
    manager.responseSerializer = [AFHTTPResponseSerializer serializer];

    NSURL *URL = [NSURL URLWithString:@"http://www.bom.gov.au/cgi-bin/wrap_fwo.pl?IDQ60359.html"];
    NSURLRequest *request = [NSURLRequest requestWithURL:URL];

    NSURLSessionDataTask *dataTask = [manager dataTaskWithRequest:request completionHandler:^(NSURLResponse *response, id responseObject, NSError *error) {
        if (error) {
            NSLog(@"Error: %@", error);
        } else {

            // Code that passes my web service data into an NSArray list

            // self->sensors = locations;
            NSArray *newObjects = [NSArray arrayWithObjects:@"key4",@"key5",@"key6",nil];

            [self->sensors addObjectsFromArray:newObjects];


            NSLog(@"Try to reload the tableView");
            [self.tableView reloadData];

        }
    }];
    [dataTask resume];


}

Upvotes: 1

Related Questions