如何使用Firebase iOS获取未读消息

时间:2018-08-31 20:51:00

标签: ios swift firebase

我正在尝试创建一种方法,以在用户收到新消息时显示,但是当我使用Firebase过滤器时,它也会分配给其他行

Firebase结构为

"users" : {
      "4" : {
        "address" : "Street",
         "id" : "198",


 "messages" : {
    "-LHf9ifs5Hcfu0VGKSsc" : {
      "forUid_status" : "198_read",
      "fromId" : "4",
      "read" : true,
      "text" : "1",
      "timestamp" : 1531880074,
      "toId" : "198"
    }



 "user-messages" : {
    "4" : {
      "43" : {
        "-LKxkblwwS5FgJR3RhuI" : 1
      }
    },

我将此方法放在cellForRowAt indexPath中

 let messagesRef = Database.database().reference().child("messages")
                let uid = message.toId
                let status = "unread"
                let queryString = uid! + "_" + status

                let ref = messagesRef.queryOrdered(byChild: "forUid_status").queryEqual(toValue: queryString)
                ref.observe(.value, with: { snapshot in
                    if snapshot.exists() {
                        let count = snapshot.childrenCount
                        cell.messagesCount.isHidden = false

                    } else {


                        print("no unread messages")
                    }
                })

有什么办法做到这一点?我研究了其他问题,但找不到解决方法。

0 个答案:

没有答案