title: Update number of unread messages in Firebase using Swift
body: <p>I am trying to implement chat in my app.
  When a user sends a message to another user, I want to add 1 to a counter of "unread number of messages" saved in Firebase.</p>
  
  <p>This is my Firebase tree:</p>
  
  <p><a href="https://i.stack.imgur.com/2VF7v.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/2VF7v.jpg" alt="Firebase Tree"></a></p>
  
  <p>The field "newMessage" is the counter for how many unread msgs I have.
  After "Messages", there is the Contact ID (The one the msg is being sent to), then the Sender ID (The one that send a msg to this contact) and then "newMessage" is how many unread messages there are from Sender to Contact.</p>
  
  <p>I have written this function to add a new unread msg:</p>
  
  <pre><code>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
              }
  
          })
      }
  </code></pre>
  
  <p>And this code doesn't seem to work properly. I have tried a couple of iterations on it, using either <code>ref.observeSingleEvent</code>, and also tried using <code>.ChildAdd</code>, and now I have reached an iteration where I'm trying to cast the snapshot into a dictionary and getting the value from there.</p>
  
  <p>All my function iterations failed. I am having trouble with properly reading the value and parsing it into Int. It reads wrong information, as can be seen here :</p>
  
  <p><a href="https://i.stack.imgur.com/rfGYe.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/rfGYe.jpg" alt="Wrong information read"></a></p>
  
  <p>The function that is activated once "Send" button is pressed in chat :</p>
  
  <pre><code>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()
  
  
          })
  
  
      }
  </code></pre>
  
  <p>Chat room initialization:</p>
  
  <pre><code>private func CreateChatRoom(creatorID: String, creatorName: String ,contactID: String, contactName: String)
      {
          let infoForCreator = ["userName": contactName, "newMessage" : String(0)]
          let infoForContact = ["userName": creatorName, "newMessage" : String(0)]
  
  
  
          let childUpdates = ["Messages/\(creatorID)/\(contactID)/": infoForCreator,
                              "Messages/\(contactID)/\(creatorID)/": infoForContact,
                              "ChatRoomsLite/\(creatorID)/\(contactID)/": infoForCreator,
                              "ChatRoomsLite/\(contactID)/\(creatorID)/": infoForContact
                              ]
  
          Constants.refs.databaseRoot.updateChildValues(childUpdates)
      }
  </code></pre>
  
  <p>What is the proper way of implementing this function ? Help would be greatly appreciated.</p>