FIRDatabaseReference messagesQuery.queryEqualToValue不返回

时间:2016-08-23 09:23:37

标签: swift firebase

我正在使用JSQMessagesViewController firechat

以下在快照中返回null

private func observeMessages() {

    let messagesQuery = messageRef.queryLimitedToLast(50)

    let query = messagesQuery.queryEqualToValue("100", childKey: "recipient")

    query.observeEventType(.ChildAdded, withBlock: { snapshot in

        let id = snapshot.value!["sender"] as! String
        let text = snapshot.value!["text"] as! String
        let recipient = snapshot.value!["recipient"] as! String
        self.addMessage(id, text: text, recipient:recipient)
        self.finishReceivingMessage()
    })
}

以下返回数据

private func observeMessages() {

    let messagesQuery = messageRef.queryLimitedToLast(50)

    messagesQuery.observeEventType(.ChildAdded, withBlock: { snapshot in

        let id = snapshot.value!["sender"] as! String
        let text = snapshot.value!["text"] as! String
        let recipient = snapshot.value!["recipient"] as! String
        self.addMessage(id, text: text, recipient:recipient)
        self.finishReceivingMessage()
    })
}

我需要按收件人进行过滤,我的firebase数据库树如下所示

enter image description here

我一直在搜索google和SO,但是找不到任何解决方案

1 个答案:

答案 0 :(得分:2)

您使用的queryEqualToValue变体并不能满足您的需求。

而是使用它:

let query = messagesQuery. queryOrderedByChild("recipient").queryEqualToValue("100")