使用Swift更新Firebase中未读邮件的数量

时间:2018-03-23 15:06:24

标签: ios swift firebase chat

我正在尝试在我的应用中实现聊天。 当用户向其他用户发送消息时,我想在Firebase中保存的“未读消息数量”计数器中添加1。

这是我的Firebase树:

Firebase Tree

字段“newMessage”是我有多少未读消息的计数器。 在“消息”之后,有联系人ID(发送消息的那个),然后是发件人ID(向该联系人发送消息的那个)然后“newMessage”是发件人有多少未读消息联系。

我已经编写了这个函数来添加一个新的未读信息:

RegisterCustomer

此代码似乎无法正常工作。我已尝试使用private func AddNewMsgTick(contactID: String) { let ref = Constants.refs.databaseChats.child(contactID).child(senderId) ref.observe( .value, with: {(snapshot) in let dataDic = snapshot.value as? NSDictionary if let currentNewMsgs = dataDic!["newMessage"] as? Int { let newIntMsg = currentNewMsgs + 1 ref.setValue(newIntMsg) // This isn't right and should be updated } }) } 进行了几次迭代,并尝试使用ref.observeSingleEvent,现在我已经到了迭代,我正在尝试将快照转换为字典并获取来自那里的价值。

我的所有函数迭代都失败了。我在正确读取值并将其解析为Int时遇到问题。它读取错误信息,如下所示:

Wrong information read

在聊天中按下“发送”按钮后激活的功能:

.ChildAdd

聊天室初始化:

override func didPressSend(_ button: UIButton!, withMessageText text: String!, senderId: String!, senderDisplayName: String!, date: Date!)
    {
        Constants.refs.databaseChats.child(senderId).observeSingleEvent(of: .value, with: {(snapshot) in

            if !snapshot.hasChild(self.contactID!)
            {
                self.CreateChatRoom(creatorID: senderId, creatorName: senderDisplayName, contactID: self.contactID!, contactName: self.contactDisplayName!)
            }

            let stringMsgPostedOn = Constants.GetCurrentDateTimeString()
            let senderRef = Constants.refs.databaseChats.child(senderId).child(self.contactID!).child("Messages").child(stringMsgPostedOn)
            let contactRef = Constants.refs.databaseChats.child(self.contactID!).child(senderId).child("Messages").child(stringMsgPostedOn)

            let senderRefLite = Constants.refs.databaseChatsLite.child(senderId).child(self.contactID!)
            let contactRefLite = Constants.refs.databaseChatsLite.child(self.contactID!).child(senderId)

            let message = ["userID": senderId, "userName": senderDisplayName, "text": text]
            let messageLite = ["userName": self.contactDisplayName, "lastMessage": text]

            // Make all this one transaction
            senderRef.setValue(message)
            senderRefLite.setValue(messageLite)

            contactRef.setValue(message)
            contactRefLite.setValue(messageLite)
            self.AddNewMsgTick(contactID: self.contactID!)
            self.finishSendingMessage()


        })


    }

实现此功能的正确方法是什么?非常感谢帮助。

2 个答案:

答案 0 :(得分:1)

像这样重构你的CreateChatRoom功能:

    let infoForCreator = ["userName": contactName, "newMessage" : 0]
    let infoForContact = ["userName": creatorName, "newMessage" : 0]

并尝试更改AddNewMsgTick以使用:

ref.observeSingleEvent

答案 1 :(得分:0)

我建议简化你的代码。您拥有的结构很好,因为您知道需要更新的数据的具体路径,请使用它。

假设Firebase结构

messages
   contact_0
      sender_0
         messages
            ...
         newMessage: 0
      sender_1
         messages
            ..
         newMessage: 5

如果我们想增加sender_0的消息计数器,只需访问并更新它的路径,如下所示:

let contactRef = self.ref.child("messages").child("contact_0")
let newMessagesRef = contactRef.child("sender_0").child("newMessage")
newMessagesRef.observeSingleEvent(of: .value, with: { snapshot in
    if let currentValue = snapshot.value as! Int? {
        let updatedValue = currentValue + 1
        ref.setValue(updatedValue)
    }
})

非常重要要注意,在你的问题中,newMessage被存储为字符串“0”(不是零,它是一个字符串),如果你想存储值,存储相反,它们就像是Int。