我正在开发一个 iOS 应用程序,并且我在 Firebase 的登录演示应用程序之上构建了我的项目。我可以通过 Facebook 进行身份验证,并与 Firebase 进行良好的通信。当我按下注销按钮时,运行的代码如下:
- (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];
}
它似乎成功了。一切都会重置,我的 tableView 也被清除。当我重新登录时,我会获取与我的 FB 凭据关联的数据,并且它会填充,一切都很好。我有一个 textField 和一个按钮,当我按下按钮时,textField 的文本将保存到 firebase 并在本地更新。
问题当我在注销一次后尝试在简单的字符串列表中创建一个新条目时,就会出现这种情况。当我重新登录并尝试保存条目时,它会保存到 firebase 一次(这是正确的),但我的回调被调用两次!
[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);
}];
相同的对象快照在此块中出现两次,这意味着相同的对象被添加到数组和我的 tableView 中两次。如果我注销并再次登录,情况会变得更加奇怪。第三次,出现了三份副本。第四次,四个项目,等等。这是我按下添加按钮时的代码:
- (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}];
}
}
看来我可能没有完全退出 Firebase 或 FB,但我不知道还能尝试什么。
什么会导致 FEventTypeChildAdded 回调针对同一个新对象被多次调用?