I\'m working on a project that loads list data from Firebase and populates a UITableView. While I see the snapshots being called from my firebase instance, they don\'t popul
Figured it out. Rather then trying to query the database in viewDidLoad, in which case the view already came up as a frame, I moved the query to the time the nib was loaded but before the frame became visible. Here's the code, expanded upon:
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
//link the CloudViewController with its View
NSBundle *appBundle = [NSBundle mainBundle];
self = [super initWithNibName:@"FoodTruckViewController" bundle:appBundle];
if (self) {
self.sampleTableView.delegate = self;
self.sampleTableView.dataSource = self;
[self loadDataFromFirebase];
[self.sampleTableView reloadData];
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
[self.sampleTableView reloadData];
}
-(void)loadDataFromFirebase
{
handles = [[NSMutableArray alloc] init];
Firebase* listRef = [[Firebase alloc] initWithUrl:@"https://wheresthatfoodtruck.firebaseIO.com/foodtrucks"];
[listRef observeEventType:FEventTypeChildAdded withBlock:^(FDataSnapshot *snapshot) {
NSLog(@"%@", snapshot.value);
[handles addObject:snapshot.value];
}];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#pragma mark - Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [handles count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
[cell.textLabel setText:[handles objectAtIndex:indexPath.row]];
return cell;
}
#pragma mark - Table view delegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
// Navigation logic may go here. Create and push another view controller.
/*
<#DetailViewController#> *detailViewController = [[<#DetailViewController#> alloc] initWithNibName:@"<#Nib name#>" bundle:nil];
// ...
// Pass the selected object to the new view controller.
[self.navigationController pushViewController:detailViewController animated:YES];
*/
}
@end
This version of the code populates the table before it becomes visible, but won't update from changes to the table in real time. I think the next step will be to make a call to the ChildChanged function form firebase in the viewDidLoad method to keep updates of any changes on the firebase side.