mardi 24 février 2015

Firebase FEventTypeChildAdded callback gets called multiple times for the same new object

I am working on an iOS app and I have built my project on top of Firebase's login-demo app. I can authenticate with Facebook, and communicate with Firebase just fine. When I press the logout button, this is the code that is run:



- (void)logoutButtonPressed
{
// logout of Firebase and set the current user to nil
[self.simpleLogin logout];
[self.ref unauth]; //Added this
[self updateUIAndSetCurrentUser:nil];
[self.items removeAllObjects];
[self.tableView reloadData];
}


And it appears to do the trick. Everything resets and my tableView is cleared. When I log back in, I get the data associated with my FB credentials and it populates and everything is great. I have a textField and a button and when I push the button the textField's text gets saved to firebase and updates locally.


The problem comes when I try and make a new entry to my simple list of strings after I have logged out once already. When I log back in, and try and save an entry, it gets saved to firebase once (which is correct), but my callback gets called twice!



[ref observeEventType:FEventTypeChildAdded withBlock:^(FDataSnapshot *snapshot) {
// Add the chat message to the array.
if (![snapshot.value isEqual:[NSNull null]]) {
[self.items addObject:snapshot.value[@"text"]];
}

// Reload the table view so the new message will show up.
[self.tableView reloadData];
} withCancelBlock:^(NSError *error) {
NSLog(@"%@", error.description);
}];


The same object snapshot shows up in this block twice, which means the same objects gets added twice to the array and my tableView. It gets even more strange if I logout and back in again. The third time, three copies show up. The fourth time, four items, etc. Here is the code for when I push the add button:



- (IBAction)submitButtonPressed {
if ([self.currentUser.provider isEqualToString:@"facebook"]) {
Firebase *postRef = [[[self.ref childByAppendingPath:@"users"] childByAppendingPath:self.currentUser.uid] childByAppendingPath:@"posts"];

NSString *statusText = [NSString stringWithFormat:@"Logged in as %@ (Facebook)",
self.currentUser.providerData[@"displayName"]];

[[postRef childByAutoId] setValue:@{@"name" : statusText, @"text": self.textField.text}];
}
}


It seems like I may not be completely logging out of Firebase or FB, but I don't know what else to try.


What would cause the FEventTypeChildAdded callback to get called multiple times for the same new object?


Aucun commentaire:

Enregistrer un commentaire