火力地堡FEventTypeChildAdded回调函数被调用多次为同一个新对象 [英] Firebase FEventTypeChildAdded callback gets called multiple times for the same new object

查看:191
本文介绍了火力地堡FEventTypeChildAdded回调函数被调用多次为同一个新对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我工作的iOS应用程序,我已经建立了我对火力地堡的登录,演示应用程序的顶部项目。我可以与Facebook进行身份验证,只是罚款与火力地堡沟通。当我preSS注销按钮,这是运行code:

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];
}

和它似乎做的伎俩。一切都将重置和我的tableView被清除。当我重新登录,我得到我的FB凭据相关数据,并将其填充,一切是伟大的。我有一个文本框和一个按钮,当我按下按钮文本域的文本被保存到火力点,并在本地更新。

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);
        }];

的相同的对象的快照在该块中示出了两次,这意味着相同的对象被添加两次到阵列和我的tableView。如果我注销并重新它变得更加诡异。第三次,三份出现。第四次,四个项目等这里是code,当我按下Add按钮:

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}];
    }
}

好像我可能不会完全退出火力地堡或FB的,但我不知道还有什么尝试。

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

什么会导致FEventTypeChildAdded回调得到调用多次为同一个新对象?

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

推荐答案

我从来没有用过的火力地堡的iOS SDK,但它以类似的方式最有可能的工作方式与其他的SDK。

I've never used the Firebase iOS SDK, but it most likely works in a similar way to the other SDKs.

如果是这样的话,你注册了侦听器模块,当用户注销时保持注册。然后,当用户再次登录,您注册第二个事件侦听器。所以,从这一刻起,你的code座会为每个添加的儿童执行两次。

If that is the case, the listener block that you registered, stays registered when the user logs out. Then when the user logs in again, you are registering a second event listener. So from that moment on, your code block will execute twice for every added child.

您应该注销/取消该事件侦听器,当用户注销(的https:/ /www.firebase.com/docs/ios/api/#firebase_removeAllObservers ),或者根本就没有,如果你之前已经注册的他们再次重新进行注册。

You should unregister/cancel the event listeners when the user logs out (https://www.firebase.com/docs/ios/api/#firebase_removeAllObservers) or simply not re-register them again if you've already registered them before.

查看火力地堡指南适用于iOS,特别是部分上分离块:的https://www.firebase.com/docs/ios/guide/retrieving-data.html#section-detaching

See the Firebase guide for iOS, specifically the section on detaching blocks: https://www.firebase.com/docs/ios/guide/retrieving-data.html#section-detaching

这篇关于火力地堡FEventTypeChildAdded回调函数被调用多次为同一个新对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆