                 if (error != nil) {
                     print("Data could not be saved.")
                 } else {
-                    self.channelRef?.runTransactionBlock({
-                        (currentData:FMutableData!) in
-                        let unreadCountData = currentData.childDataByAppendingPath("unreadCount")
-                        let totalCountData = currentData.childDataByAppendingPath("totalCount")
-                        
-                        var totalCount = totalCountData.value as? Int
-                        if totalCount == nil {
-                            totalCount = 0
-                        }
-                        totalCountData.value = totalCount! + 1
-                        
-                        var unreadCount = unreadCountData.value as? Int
-                        if unreadCount == nil {
-                            unreadCount = 0
-                        }
-                        unreadCountData.value = unreadCount! + 1
-                        
-                        
-                        return FTransactionResult.successWithValue(currentData)
-                    })
-                    
+                    self.updateTotalAndUnreadCount()
                     print("Data saved successfully!")
                 }
             })

            weakSelf.chatMessages.append(chatMessage)
            weakSelf.tableView.insertRowsAtIndexPaths([NSIndexPath(forRow: weakSelf.chatMessages.count-1, inSection: 0)], withRowAnimation: .Automatic)
            weakSelf.tableView.scrollToRowAtIndexPath(NSIndexPath(forRow: weakSelf.chatMessages.count-1, inSection: 0), atScrollPosition: .Bottom, animated: true)
            
            }, withCancelBlock: { error in
                print(error.description)
        })
        
        channelRef?.observeSingleEventOfType(.Value, withBlock: { snapshot in

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    
    func updateReadMsgCount(forRefrence reference: Firebase) {
        reference.runTransactionBlock({ (messageData) -> FTransactionResult! in
            let read = messageData.childDataByAppendingPath("read")
            read.value = true
            return FTransactionResult.successWithValue(messageData)
            }) { (error, commited, snapshot) -> Void in
                if error == nil && commited {
                    self.channelRef?.runTransactionBlock({
                        (currentData:FMutableData!) in
                        let unreadCountData = currentData.childDataByAppendingPath("\(self.userName ?? "")_unreadCount")
                        let totalCountData = currentData.childDataByAppendingPath("\(self.userName ?? "")_totalCount")
                        
                        var totalCount = totalCountData.value as? Int
                        if totalCount == nil {
                            totalCount = 0
                        }
                        totalCountData.value = totalCount! - 1
                        
                        var unreadCount = unreadCountData.value as? Int
                        if unreadCount == nil {
                            unreadCount = 0
                        }
                        unreadCountData.value = unreadCount! - 1
                        return FTransactionResult.successWithValue(currentData)
                    })
                }
        }
    }
    
    func updateTotalAndUnreadCount() {
        self.channelRef?.runTransactionBlock({
            (currentData:FMutableData!) in
            let unreadCountData = currentData.childDataByAppendingPath("\(self.chat?.user ?? "")_unreadCount")
            let totalCountData = currentData.childDataByAppendingPath("\(self.chat?.user ?? "")_totalCount")
            
            var totalCount = totalCountData.value as? Int
            if totalCount == nil {
                totalCount = 0
            }
            totalCountData.value = totalCount! + 1
            
            var unreadCount = unreadCountData.value as? Int
            if unreadCount == nil {
                unreadCount = 0
            }
            unreadCountData.value = unreadCount! + 1
            
            
            return FTransactionResult.successWithValue(currentData)
        })
        
    }
    
    @IBAction func sendMesageButtonTapped(sender: UIButton) {
