I'm new to iOS development and wrestling with UITableViews.
My problem is that I'm populating my UITableView with data from an external server, but due to multithreading it's not waiting until the data arrives before loading the table view.
My current idea is to reload the table view when the data loads.
Earlier in same class DailyBreakdown.c, I reload the table view with this code:
-(void)viewWillAppear:(BOOL)animated
{
[[self class] getAllActivities];
[super viewWillAppear:animated];
[self makeObjects];
[self.tableView reloadData];
}
So on the callback when my data loads (using Restkit), I try to call [self.tableView reloadData] again, but I get the errors:
Definition of 'struct objc_class' must be imported from module 'ObjectiveC.runtime' before it is required
Implicit conversion of Objective-C pointer type 'Class' to C pointer type 'struct objc_class *' requires a bridged cast
Here's the method where I return the Activity objects from the API:
+(NSArray *)getAllActivities{
if (allActivities == nil) {
// Load the object model via RestKit
//allActivities = [[NSMutableArray alloc] initWithObjects:@"Workout", @"Baked cake", @"Read for HR", nil];
allActivities = [[NSMutableArray alloc] init];
RKObjectManager *objectManager = [RKObjectManager sharedManager];
[objectManager getObjectsAtPath:@"/activities"
parameters:nil
success:^(RKObjectRequestOperation *operation, RKMappingResult *mappingResult) {
//allActivities = [mappingResult array];
allActivities = [NSMutableArray arrayWithArray: [mappingResult array]];
[[self class] makeObjects];
/*** THIS LINE IS THE PROBLEM **/
[self.tableView reloadData];
}
failure:^(RKObjectRequestOperation *operation, NSError *error) {
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Error"
message:[error localizedDescription]
delegate:nil
cancelButtonTitle:@"OK"
otherButtonTitles:nil];
[alert show];
NSLog(@"Hit error: %@", error);
}];
}
return allActivities;
}
So, why can't I call [self.tableView reloadData] as before? How would I do this from inside my class method?
Also, is there a better way to accomplish this besides reloading the tableview? Maybe threadlocking so that allActivities doesn't return nil when the view is loaded? Any ideas are welcome.
Thanks!