对于同一个新对象,Firebase FEventTypeChildAddedcallback被多次调用

我正在开发iOS应用程序,并且已经在Firebase的login演示应用程序之上构build了我的项目。 我可以使用Facebook进行身份validation,并与Firebase进行通信。 当我按下注销button时,这是运行的代码:

- (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被清除。 当我重新login时,我得到了与我的FB凭据相关的数据,并且它填充并且一切都很好。 我有一个textField和一个button,当我按下button时,textField的文本被保存到firebase并在本地更新。

问题出现在我已经注销一次之后尝试向我简单的string列表添加新条目时。 当我重新login并尝试保存一个条目时,它会被保存到firebase一次(这是正确的),但是我的callback会被调用两次!

 [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。 如果我注销并重新login,会变得更加奇怪。 第三次出现三份。 第四次,四个项目等这里是我按下添加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}]; } } 

似乎我可能不完全退出Firebase或FB,但我不知道还有什么可以尝试的。

什么会导致相同的新对象多次调用FEventTypeChildAddedcallback?

我从来没有使用Firebase iOS SDK,但它很可能与其他SDK类似。

如果是这种情况,那么您注册的侦听器块将在用户注销时保持注册状态。 然后,当用户再次login时,您正在注册第二个事件侦听器。 所以从那一刻起,你的代码块将为每个添加的子项执行两次。

当用户注销时( https://www.firebase.com/docs/ios/api/#firebase_removeAllObserver ),您应该取消注册/取消事件侦听器,或者如果您之前已经注册了它们,则不需要重新注册它们。

请参阅iOS的Firebase指南,特别是有关分离块的部分: https : //www.firebase.com/docs/ios/guide/retrieving-data.html#section-detaching