【问题标题】:Firebase callback does not fireFirebase 回调不会触发
【发布时间】:2014-01-26 12:56:54
【问题描述】:

我有这个 Firebase 参考,可以通过传递上次读取消息的优先级来检索所有待处理(未读)消息。我在聊天室里做这个加载。

NSNumber* lastPriority = [self getLastPriority];
__block FirebaseHandle handle = [[msgListRef queryStartingAtPriority:lastPriority] observeEventType:FEventTypeValue withBlock:^(FDataSnapshot *snapshot) {

    // here I will get all the pending messages and add them to my local message list and reload the UITableView
    [self addMessageToList: snapshot]
    [msgListRef removeObserverWithHandle:handle];
    // Then I call the below function to keep listening for new messages when the chat is in progress
    [self addMessageListener];

}];



- (void) addMessageListener{
        msgListRef = [[Firebase alloc] initWithUrl:messageListUrl];
        NSNumber* lastPriority = [self getLastPriority];
        [[msgListRef queryStartingAtPriority:lastPriority] observeEventType:FEventTypeChildAdded withBlock:^(FDataSnapshot *snapshot) {

            [self addMessageObject:snapshot.value];
            // Here I will reload the table view. This one is supposed to fire for each       
            //message since I used FEventTypeChildAdded. 
            //*** BUT THIS CALLBACK IS NEVER FIRED ***

        }];
}

知道为什么在我之前已经调用过 FEventTypeValue 时,使用 FEventTypeChildAdded observerType 的第二个回调永远不会被触发吗?如果我不使用 FEventTypeValue 阅读所有内容,它会起作用。但在这种情况下,当用户进入聊天室时,每条待处理消息都会调用我的 UITableView 重新加载。

【问题讨论】:

  • 您不应该使用 lastPriority 而不是 lastMessage.priority 吗?另外,优先级的价值是什么?有多少条消息具有该优先级?
  • 这是问题中的错字。我会编辑它。优先级是一个运行编号,每次收到新消息时都会递增。它也存储在 firebase 位置。
  • 我还要仔细检查您传递给子事件处理程序的优先级是否与值事件处理程序相同。 addMessageToList 是否以某种方式修改了最后一个优先级?
  • 很难从 sn-p 中判断您的代码试图做什么。 addMessageToListaddMessageObject 有什么区别? msgListRef 的值是否在变化?无论如何,如果你使用observeSingleEventOfType 而不是observeEventType,它可能会简化一些事情,所以你可以摆脱removeObserverWithHandle 调用(我实际上没有看到它有问题,但很难推理关于它的作用,因为它使用共享变量并在块内执行)。
  • 嗨,克里斯,我希望使用observeSingleEventOfType,但这样我就无法从特定的优先级进行查询。我看到了这一点,因为这是在 firebase 文档中给出的关于如何立即删除观察者的示例。 addMessageToList 将遍历所有项目并添加到列表中。 addMessageObject 将添加单个项目。抱歉没有详细解释..

标签: ios objective-c firebase


【解决方案1】:

对不起,伙计们。问题出在我的代码上。 firebase 引用 (msgListRef) 在我没有注意到的另一个函数中重新初始化。

【讨论】:

    猜你喜欢
    • 2010-10-09
    • 1970-01-01
    • 2015-10-27
    • 1970-01-01
    • 2019-10-24
    • 1970-01-01
    • 1970-01-01
    • 2019-05-19
    • 2011-11-23
    相关资源
    最近更新 更多