// Create array of data model
var dataArray = [Data]()
// Create retrieve data func
func retrieveData() {
// Get reference to where data is stored
let referenceDB = Database.database().reference().child("DataSetName")
// Observe for changes
referenceDB.observe(.childAdded) { (snapshot) in
let snapshotValue = snapshot.value as! Dictionary<String,String>
// Save snapshot values
let text = snapshotValue["Body"]!
let sender = snapshotValue["Sender"]!
// Create data model and save snapshot values in
let data = Data()
data.body = text
data.sender = sender
// Append new data model in data model array
self.dataArray.append(data)
// IF TABLEVIEW: Reload tableView data to display changes
self.tableView.reloadData()
}
}
// Call retrieveMessages func in viewDidLoad
retrieveData()