Joshua Hart Joshua Hart - 1 month ago 10
iOS Question

UITableView repeating Firebase Data

I'm getting repeated content from Firebase, and I can't seem to figure out what I am doing wrong. In firebase I have 6 posts. The tableview is populating 6 cells, but all 6 cells have the same data, and the 5 other posts are not there.

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath

{
RankingsCell *cell = [tableView dequeueReusableCellWithIdentifier:@"RankingsCell"];
self.ref = [[FIRDatabase database] reference];
posts = [ref child:@"posts"];

[posts observeEventType:FIRDataEventTypeValue withBlock:^(FIRDataSnapshot *snapshot)
{
for (snapshot in snapshot.children)
{
NSString *username = snapshot.value[@"Name"];
NSString *date = snapshot.value[@"Date"];
NSString *combatPower = snapshot.value[@"Combat Power"];
NSString *pokemon = snapshot.value[@"Pokemon"];
NSString *pokemonURL = snapshot.value[@"Pokemon Image"];
NSString *picURL = snapshot.value[@"Profile Picture"];
int CP = [combatPower intValue];

cell.usernameOutlet.text = username;
cell.dateOutlet.text = date;
cell.combatPowerLabel.text = [NSString stringWithFormat:@"COMBAT POWER: %d", CP];
cell.pokemonLabel.text = pokemon;
[cell downloadUserImage:picURL];
[cell downloadPokemonImage:pokemonURL];
}
}
withCancelBlock:^(NSError * _Nonnull error)
{
NSLog(@"%@", error.localizedDescription);
}];
return cell;
}

Answer

cellForRowAtIndex: method is called for "every" cell, so you're not supposed to do your database work in there, it's just responsible for creating "one" cell at a time.

So, move your observeEventType: call in viewDidLoad: or viewDidAppear: like:

- (void)viewDidLoad
{
    [super viewDidLoad];

    self.ref = [[FIRDatabase database] reference];
    posts = [ref child:@"posts"];

    [posts observeEventType:FIRDataEventTypeValue withBlock:^(FIRDataSnapshot *snapshot)
     {
         self.allSnapshots = [NSMutableArray array];

         for (snapshot in snapshot.children)
         {
                 [self.allSnapshots addObject:snapshot];
         }

         [self.tableView reloadData]; // Refresh table view after getting data
     } // .. error ...
}

And in numberOfRowsForInSection:

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section      
{
    return [self.allSnapshots count];
}

And in cellForRowAtIndex:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    RankingsCell *cell = [tableView dequeueReusableCellWithIdentifier:@"RankingsCell"];

    FIRDataSnapshot *snapshot = [self.allSnapshots objectAtIndex:indexPath.row]; 

    NSString *username = snapshot.value[@"Name"];
    NSString *date = snapshot.value[@"Date"];
    NSString *combatPower = snapshot.value[@"Combat Power"];
    NSString *pokemon = snapshot.value[@"Pokemon"];
    NSString *pokemonURL = snapshot.value[@"Pokemon Image"];
    NSString *picURL = snapshot.value[@"Profile Picture"];
    int CP = [combatPower intValue];

    cell.usernameOutlet.text = username;
    cell.dateOutlet.text = date;
    cell.combatPowerLabel.text = [NSString stringWithFormat:@"COMBAT POWER: %d", CP];
    cell.pokemonLabel.text = pokemon;
    [cell downloadUserImage:picURL];
    [cell downloadPokemonImage:pokemonURL];

    return cell;
}