         super.viewWillAppear(animated)
     }
 
+    deinit {
+        liveQuery?.removeObserver(self, forKeyPath: "rows")
+    }
+
     func insertNewObject(sender: AnyObject) {
-//        objects.insert(NSDate(), atIndex: 0)
-//        let indexPath = NSIndexPath(forRow: 0, inSection: 0)
-//        self.tableView.insertRowsAtIndexPaths([indexPath], withRowAnimation: .Automatic)
-
         let newChatroom = database.createDocument()
         let properties = ["type": "chatroom", "name": "from code"]
 

        let live = query.asLiveQuery()
        live.descending = true

        liveQuery = live

        // Magic line
        live.waitForRows()

        if let rows = live.rows {
            for row in rows {
                print(row)
            }
        } else if let error = live.lastError {
            print("Error")
            print(error)
        }
    }

    override func viewWillAppear(animated: Bool) {

        } catch {
            print("Could not create a new room")
        }
    }

    override func observeValueForKeyPath(keyPath: String?, ofObject object: AnyObject?, change: [NSObject : AnyObject]?, context: UnsafeMutablePointer<Void>) {
        self.tableView.reloadData()
    }

    // MARK: - Segues
