使用Swift中的Firebase计算看不见的消息 [英] Count unseen messages with Firebase in Swift

查看:128
本文介绍了使用Swift中的Firebase计算看不见的消息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试对聊天应用进行编码。

I'm trying to code a chat app.

我想计算未查看的消息数量(未见)。

I would like to count the number of messages, which are not viewed (unseen).

我的数据库如下所示:

user-messages
 |
 |_messages
    |
    |_UserID1
    |   |
    |   |_UserID2
    |     |
    |     |_ MessageID1
    |     |_ MessageID2
    |     |_ MessageID3
    |     |_ etc...
    |     
    |_UserID2
    |   |
    |   |_UserID1
    |     |
    |     |_ MessageID1
    |     |_ MessageID2
    |     |_ MessageID3
    |     |_ etc...

对于Messages对象:

And for the Messages object:

messages
 |
 |_messageID1
 |  |
 |  |_ notViewed: false / true
 |  |_ text: "Message text"
 |  |_ timestamp: 1522230692
 |  |_ etc...
 |  
 |_messageID2
 |  |
 |  |_ notViewed: false / true
 |  |_ text: "Message text"
 |  |_ timestamp: 1522230692
 |  |_ etc...

我到达时获取链接到特定用户的所有消息ID :

I arrived to get all the message ID which are linked to a specific user:

var REF_MESSAGE = Database.database().reference().child("messages")
var REF_USER_MESSAGE = Database.database().reference().child("user-messages").child("messages")


func fetchUnviewedMessages(withId id: String, completion: @escaping (Int) -> Void ) {

      REF_USER_MESSAGE.child(id).observe(.childAdded) { (snapshot) in

         let userId = snapshot.key as String

         self.REF_USER_MESSAGE.child(id).child(userId).observe(.childAdded, with: { (snapshot) in
            let messageId = snapshot.key

                // This part is where I have a problem
                self.fetchMessageNotViewed(messageId, completion: { (nbMessageNotRead)  in
                   completion(nbMessageNotRead)
                })
             })
          }
    }

但我不知道我需要对该列表做些什么,以观察看不见的消息数量...

But I don't know what I need to do with that list, to observe the number of unseen messages ...

我试过了,但没有成功:

I tried that, but without success:

func fetchMessageNotViewed(_ messageId: String, completion: @escaping (Int) -> Void) {
      guard let currentUid = Api.User.CURRENT_USER?.uid else {
         return
      }

      REF_MESSAGE.child(messageId).queryOrdered(byChild: "notViewed").queryEqual(toValue: true).observe(.value, with: { (snapshot) in

         let count = Int(snapshot.childrenCount)
         print(count)
      }, withCancel: nil)
}


推荐答案

我们可以利用复合值来轻松处理这个问题。

We can leverage a compound value to handle this fairly easily.

对Firebase结构消息节点进行简单更改:

Make a simple change to the Firebase structure messages node:

messages
 |
 |_messageID1
 |  |
 |  |_ notViewed: false / true
 |  |_ text: "Message text"
 |  |_ timestamp: 1522230692
 |  |_ forUid_status: "UserId1_unread"   //<- Compound value
 |  |_ etc...
 |  
 |_messageID2
 |  |
 |  |_ notViewed: false / true
 |  |_ text: "Message text"
 |  |_ timestamp: 1522230692
 |  |_ forUid_status: "UserId1_read"   //<- Compound value
 |  |_ etc...

然后一个简单的查询将显示UserId1未读消息的数量

then a simple query will reveal a count of UserId1 unread messages

let messagesRef = self.ref.child("messages")
let uid = "UserId1"
let status = "unread"
let queryString = uid + "_" + status //results in a string UserId1_unread

let ref = messagesRef.queryOrdered(byChild: "forUid_status").queryEqual(toValue: queryString)
ref.observe(.value, with: { snapshot in
    if snapshot.exists() {
        let count = snapshot.childrenCount
        print("unread messages: \(count)")
    } else {
        print("no unread messages")
    }
})

针对上述Firebase运行此代码会导致

running this code against the above Firebase results in

unread messages: 1

这也可能有助于加载未读消息,因为您不仅可以获得他们的计数但是将观察者添加到该节点也将通知应用程序是时候为UserId1

This may also help with the loading of unread messages as not only can you get their count but adding the observer to that node will also notify the app any time a new unread message is added for UserId1

这篇关于使用Swift中的Firebase计算看不见的消息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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