         }
         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)
+            }
         }
     }
     

            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

                if (error != nil) {
                    print("Data could not be saved.")
                } else {
                    self.updateTotalAndUnreadCount()
                    print("Data saved successfully!")
                }
            })
