【发布时间】:2015-02-22 20:34:19
【问题描述】:
我正在开发一个 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 回调?
【问题讨论】:
标签: ios objective-c facebook authentication firebase