         }
         UIView.animateWithDuration(timeInterval) { () -> Void in
             self.view.layoutIfNeeded()
-            self.tableView.scrollToRowAtIndexPath(NSIndexPath(forRow: self.chatMessages.count - 1, inSection: 0), atScrollPosition: .Bottom, animated: true)
+            if self.chatMessages.count > 0 {
+                self.tableView.scrollToRowAtIndexPath(NSIndexPath(forRow: self.chatMessages.count - 1, inSection: 0), atScrollPosition: .Bottom, animated: true)
+            }
         }
     }
     

                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)
                    })
                    
                    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)
            if from != weakSelf.userName {
                guard let messageRef = weakSelf.ref else {return}
                weakSelf.updateReadMsgCount(forRefrence: messageRef.childByAppendingPath(id))
            }
            
            
            
            }, withCancelBlock: { error in
                print(error.description)
        })
        
        ref?.observeEventType(.ChildRemoved, withBlock: { [weak self]
            snapshot in
            guard let weakSelf = self else {return}
            print(snapshot.value)
            let id = snapshot.key
            let index = weakSelf.chatMessages.indexOf {$0.id == id}
            if index != nil && index! >= 0 && index! < weakSelf.chatMessages.count {
                weakSelf.chatMessages.removeAtIndex(index!)
                weakSelf.tableView.deleteRowsAtIndexPaths([NSIndexPath(forRow: index!, inSection: 0)], withRowAnimation: .Automatic)
            }
        })
        
        channelRef?.observeSingleEventOfType(.Value, withBlock: { snapshot in
