在 Swift 中使用 Firebase 计算看不见的消息 [英] Count unseen messages with Firebase in Swift
问题描述
我正在尝试编写一个聊天应用程序.
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屋!